Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy file with swift

Tags:

ios

swift

restkit

I am copying a file database with this code

try fileManager.copyItem(atPath: storeURL.path, toPath: storeCopyURL.path)

I can see that a new sqlite database is created

later, when I try to use this function

try! sharedInstance.managedObjectStore.addSQLitePersistentStore(atPath: storeURL.path, fromSeedDatabaseAtPath: storeCopyURL.path, withConfiguration: nil, options: nil)

I get an error

E restkit.core_data:RKManagedObjectStore.m:299 Failed to copy seed database from path ...

like image 280
j.doe Avatar asked Jan 25 '18 11:01

j.doe


1 Answers

To securely copy a file you should use the following extension:

extension FileManager {

    open func secureCopyItem(at srcURL: URL, to dstURL: URL) -> Bool {
        do {
            if FileManager.default.fileExists(atPath: dstURL.path) {
                try FileManager.default.removeItem(at: dstURL)
            }
            try FileManager.default.copyItem(at: srcURL, to: dstURL)
        } catch (let error) {
            print("Cannot copy item at \(srcURL) to \(dstURL): \(error)")
            return false
        }
        return true
    }

}

For anything else regarding CoreData we need more information about your code and what you are trying to do.

like image 82
sundance Avatar answered Oct 11 '22 16:10

sundance