Realm 文件的快速副本 - 不工作

标签 swift copy realm

Swift 版本 3。

如果我的 realm.default 文件不存在,我正在努力在启动时将其复制到我的应用程序中,以便我可以将其打包为分发的一部分。该文件需要可变,因此我将其复制到文档目录。

不幸的是,我得到一个文件不存在的错误。我已验证路径正确,并且 .app 中包含该文件。

话虽如此,该文件有一个白色圆圈,上面有一条线(不要输入类型),并说当我尝试打开它时,我无法在这种 mac 上打开它。我可以通过选择 show package contents 来查看内容,文件包含在其中。

上面的文字..

以下是我的 App Delegate 加载 Realm 文件的代码:

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)")
        }
    }
}

以下是错误消息(粗体显示上下文):

发生错误,详情如下: Error Domain=NSCocoaErrorDomain Code=4 “文件“default.realm”不存在。” UserInfo={NSSourceFilePathErrorKey=/Users/用户名/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Bundle/Application/B324E72E-2B5A-4A02-BC2C-FB542FDA6957/AppName .app/default.realm, NSUserStringVariant=( 复制 ), 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 “没有这样的文件或目录”}}

任何人对此有任何想法。至少,路径一直到 Users/username/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Bundle/Application/B324E72E-2B5A-4A02-BC2C-FB542FDA6957/AppName.app/存在,然后文件包含在包中。

最佳答案

因为您混淆了 pathURLRealm.Configuration.defaultConfiguration.fileURL 返回 URL 的实例。 Bundle.main.path() 返回 String 的实例。 URL 的字符串表示与 path 不同。

例如

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

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

因此您应该使用其中之一(路径或 URL)。如果您使用 path,请使用 defaultRealmPath.path 而不是 String(describing: defaultRealmPath),如下所示:

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)")
    }
}

如果您使用 URLBundle.main.url() 代替 Bundle main.path(): 让 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)")
    }
}

关于 Realm 文件的快速副本 - 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39675505/

相关文章:

python - 如何复制matplotlib图形?

ios - 列表中的重复条目

ios - 无法在 IOS 中更改应用程序角标(Badge)

arrays - 如果通过重复加倍完成,为什么调整数组大小操作的时间与 N 成正比?

ios - 如何在 Swift 中隐藏元素及其空间

python - 如何在 windows 和 linux 上制作文件的完整备份副本

ios - 遍历数组并将每个值添加到 Realm 数据库 Swift 3

java - 我怎样才能最快最简单地删除 RealmResults 列表中的所有项目?

swift - 从 osx keychain 中检索 key

ios - 使用发送自定义按钮在 UITableview 中的新行中插入数据