无法在路径上打开领域 [英] Unable to open a realm at path

查看:54
本文介绍了无法在路径上打开领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用捆绑的领域文件但没有成功.我知道我的领域文件已成功复制到我的应用程序目录,但我无法读取它.

I am trying to use a bundled realm file without success. I know that my realm file was copied successfully to my application’s Directory but I ca not read it.

致命错误:'尝试!'表达式意外引发错误:无法在路径上打开一个领域'/Users/.../Library/Developer/CoreSimulator/Devices/.../data/Containers/Data/Application/.../Documents/default-v1.realm'.请使用您的应用具有读写权限的路径."

fatal error: 'try!' expression unexpectedly raised an error: "Unable to open a realm at path '/Users/…/Library/Developer/CoreSimulator/Devices/…/data/Containers/Data/Application/…/Documents/default-v1.realm'. Please use a path where your app has read-write permissions."

 func fullPathToFileInAppDocumentsDir(fileName: String) -> String {

        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask,true)

        let documentsDirectory = paths[0] as NSString

        let fullPathToTheFile = documentsDirectory.stringByAppendingPathComponent(fileName)

        return fullPathToTheFile
    }

didFinishLaunchingWithOptions中:

  let fileInDocuments = fullPathToFileInAppDocumentsDir("default-v1.realm")

        if !NSFileManager.defaultManager().fileExistsAtPath(fileInDocuments) {

            let bundle = NSBundle.mainBundle()

            let fileInBundle = bundle.pathForResource("default-v1", ofType: "realm")

            let fileManager = NSFileManager.defaultManager()

            do {
                try fileManager.copyItemAtPath(fileInBundle!, toPath: fileInDocuments)
            } catch {  print(error)  }
        }

并设置用于默认 Realm 的配置:

And setting the configuration used for the default Realm:

var config = Realm.Configuration()
config.path = fileInDocuments

Realm.Configuration.defaultConfiguration = config

let realm = try! Realm(configuration: config)  // It fails here!!! :-)

正如文档所建议的,我也尝试通过在 Realm.Configuration 上将 readOnly 设置为 true 来直接从包路径打开它代码>对象.我不确定这是否与 Realm 相关,或者我是否忽略了文件系统的某些内容......我也尝试将文件存储在 Library 文件夹中.

As the documentation suggests, I have tried as well to open it directly from the bundle path by setting readOnly to true on the Realm.Configuration object. I am not sure if this is something related to Realm or if I am overlooking something with the file system…. I have also tried to store the file in the Library folder.

Realm 0.97.0
Xcode Version 7.1.1

推荐答案

我尝试使用 Realm 的浏览器应用程序打开领域文件,但该文件不再打开.它现在有新的权限:只写(Dropbox).因此,我决定使用文件管理器的 setAttributes 方法.我是这样做的:

I tried to open the realm file using Realm's browser app and the file does not open anymore. It has now new permissions: Write only (Dropbox). So, I decided to change the file permission back to read/write using file manager’s setAttributes method. Here is how I did it:

 // rw rw r : Attention for octal-literal in Swift "0o".
 let permission = NSNumber(short: 0o664)

  do {
    try fileManager.setAttributes([NSFilePosixPermissions:permission], ofItemAtPath: fileInDocuments)

  } catch { print(error) }

现在可以在此路径打开领域文件.

The realm file can now be open at this path.

这篇关于无法在路径上打开领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆