Swift Copy of Realm File - Not Working

﹥>﹥吖頭↗ 提交于 2019-12-24 07:49:49

问题


Swift version 3.

I am working on copying my realm.default file into my application upon start if it doesn't exist so that I can package it as part of distribution. The file needs to be mutable so I am copying it over to the documents directory.

Unfortunately I am getting an error that the file doesn't exist. I have verified that the paths are correct, and that the .app has the file in it.

With that being said, the file has a white circle with a line on it (do not enter type) and says that I can't open it on this kind of mac when I try to open it. I can see the contents by selecting show package contents and the file is contained within.

Text Above..

The following is the code from my App Delegate that loads the realm file:

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

func openRealm() {

    let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL!
    let bundleReamPath = Bundle.main.path(forResource: "default", ofType:"realm")

    if !FileManager.default.fileExists(atPath: String(describing: defaultRealmPath)) {
        do
        {
            try FileManager.default.copyItem(atPath: bundleReamPath!, toPath: String(describing: defaultRealmPath))
        }
        catch let error as NSError {
        // Catch fires here, with an NSError being thrown
        print("error occurred, here are the details:\n \(error)")
        }
    }
}

The following are the error messages (bolded for context):

error occurred, here are the details: Error Domain=NSCocoaErrorDomain Code=4 "The file “default.realm” doesn’t exist." UserInfo={NSSourceFilePathErrorKey=/Users/username/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Bundle/Application/B324E72E-2B5A-4A02-BC2C-FB542FDA6957/AppName.app/default.realm, NSUserStringVariant=( Copy ), NSDestinationFilePath=file:///Users/username/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Data/Application/565716DD-E903-409F-B7C8-20154B0DF6BA/Documents/default.realm, NSFilePath=/Users/username/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Bundle/Application/B324E72E-2B5A-4A02-BC2C-FB542FDA6957/AppName.app/default.realm, NSUnderlyingError=0x618000241bc0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

Anyone have any ideas on this. At a minimum, the path all the way up to Users/username/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Bundle/Application/B324E72E-2B5A-4A02-BC2C-FB542FDA6957/AppName.app/ exists and then the file is contained within the package.


回答1:


Because you're confusing path and URL. Realm.Configuration.defaultConfiguration.fileURL returns instance of URL. Bundle.main.path() returns instance of String. A String representation of URL isn't same as path.

e.g.

print(String(describing: defaultRealmPath))
// => file:///Users/.../Documents/default.realm

print(defaultRealmPath.path)
// => /Users/.../Documents/default.realm

So you should use either one (path or URL). If you use path, use defaultRealmPath.path instead String(describing: defaultRealmPath) like the following:

let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL!
let bundleReamPath = Bundle.main.path(forResource: "default", ofType:"realm")

if !FileManager.default.fileExists(atPath: defaultRealmPath.path) {
    do
    {
        try FileManager.default.copyItem(atPath: bundleReamPath!, toPath: defaultRealmPath.path)
    }
    catch let error as NSError {
        // Catch fires here, with an NSError being thrown
        print("error occurred, here are the details:\n \(error)")
    }
}

If you use URL, Bundle.main.url() instead Bundle main.path(): let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL!

let bundleReamPath = Bundle.main.url(forResource: "default", withExtension:"realm")!

if !FileManager.default.fileExists(atPath: defaultRealmPath.path) {
    do
    {
        try FileManager.default.copyItem(at: bundleReamPath, to: defaultRealmPath)
    }
    catch let error as NSError {
        // Catch fires here, with an NSError being thrown
        print("error occurred, here are the details:\n \(error)")
    }
}


来源:https://stackoverflow.com/questions/39675505/swift-copy-of-realm-file-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!