【问题标题】:Realm exception Error Domain=io.realm Code=2 "Operation not permitted"领域异常 Error Domain=io.realm Code=2 "Operation not allowed"
【发布时间】:2016-06-29 18:44:39
【问题描述】:

我正在努力为领域使用新的 defaultConfiguration(我想是的......)

那么我想达到什么目的。我想将默认 Realm URL 更改为基于 Apple App 组的 ID,因为我想在我的应用程序的 Today Extension 以及我的应用程序本身中使用相同的 Realm。

我找到了一个 (slightly outdated Realm tutorial for WatchKit Extension),您将以下内容放入 AppDelegate:

realmUrl: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.net.exchange.On")!
realmUrl.URLByAppendingPathComponent("db.realm")

var config = Realm.Configuration.defaultConfiguration
config.fileURL = realmUrl
Realm.Configuration.defaultConfiguration = config

这行得通。但是我从 Realm 读取的编码,在此之前工作,现在崩溃了:

这是为了适应这种变化:`let realm = try!领域()``

但现在它创建了这个漂亮的错误 ;-)

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm 
Code=2 "Operation not permitted" UserInfo={Error Code=2, 
NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/4E8402AD-89E4-4138-8B83-CA6B409BB238, 
Underlying=n/a, NSLocalizedDescription=Operation not permitted}: 
file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-703.0.18.8/src/swift/stdlib/public/core/ErrorType.swift, line 54

我有点失落。希望有人可以帮助我。 TIA 约翰

【问题讨论】:

    标签: ios swift2 realm


    【解决方案1】:

    问题在于,您在 Realm 配置中设置的路径 fileURL 是应用组容器的路径,而不是其中文件的路径。这是由于以下代码:

    let realmUrl: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.net.exchange.On")!
    realmUrl.URLByAppendingPathComponent("db.realm")
    

    NSURL.URLByAppendingPathComponent(_:) 返回一个新的 URL,而不是改变现有的 URL。将代码更改为以下内容应该会导致在 Realm 的配置中设置正确的 URL:

    let groupContainerUrl = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.net.exchange.On")!
    let realmUrl = groupContainerUrl.URLByAppendingPathComponent("db.realm")
    

    【讨论】:

    • 谢谢。就是这样。干杯,约翰
    猜你喜欢
    • 2012-01-16
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 2015-09-01
    相关资源
    最近更新 更多