2016-06-29 28 views
1

我與使用領域一個新的defaultConfiguration(我想是這樣......)境界異常錯誤域= io.realm代碼= 2「不允許操作」

那麼我儘量做到struggeling。我想將默認的Realm URL更改爲基於Apple App組的ID,因爲我想從我的應用程序的今日擴展程序以及我的應用程序本身使用相同的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 John

回答

2

問題是,您在Realm配置上設置的路徑fileURL是您的應用程序組容器的路徑,而不是其中的文件路徑。這是由於下面這段代碼:

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

NSURL.URLByAppendingPathComponent(_:)返回一個新的URL,而不是突變的存在之一。將代碼更改爲以下內容應導致在Realm的配置中設置正確的URL:

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

謝謝。而已。乾杯,約翰 –

相關問題