cocoa - 如何将默认 Realm 路径设置为 App Groups 目录

标签 cocoa swift realm

我正在尝试将默认 Realm 路径设置为 App Groups 目录。

let directory: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("groups.prasanna.appName")!  
RLMRealm.setDefaultRealmPath(directory.absoluteString!)  
println(RLMRealm.defaultRealmPath())  

应用程序崩溃并出现以下错误

Terminating app due to uncaught exception 'RLMException', reason: 'open() failed: Operation not permitted'

我该如何解决这个问题?

最佳答案

您设置的默认 Realm 路径是您的容器目录。你必须附加一个文件名才能工作:

let directory: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("groups.prasanna.appName")!
let realmPath = directory.path!.stringByAppendingPathComponent("db.realm")
RLMRealm.setDefaultRealmPath(realmPath)
println(RLMRealm.defaultRealmPath()) // should be realmPath

关于cocoa - 如何将默认 Realm 路径设置为 App Groups 目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27355557/

相关文章:

iOS CSR 证书未正确生成

swift - 如何获取通知中心显示的推送通知

ios - iCloud自动备份的.realm文件

iPhone HTTP REST 请求/响应

objective-c - 了解 Cocoa 应用程序中的 gc-roots 信息

objective-c - 登录窗口通知或获取有关用户注销的通知

ios - Realm Swift 单元测试错误 : Missing required modules: 'Realm.Private' , 'Realm'?

ios - 删除观察者: forKeyPath is not working

ios - 使用 UserDefaults 存储点击游戏的分数(Swift 5)

swift3 - 使用 BETWEEN 在 Realm 中查找对象