在iOS

2014-09-25 59 views
2

境界realmWithPath方法崩潰當我打電話:在iOS

[RLMRealm realmWithPath:@"example.realm"] 

它崩潰和原木:

終止應用程序由於未捕獲的異常 'RLMException',原因:「open()的失敗:不允許操作'

除了使用default.realm和[RLMRealm defaultRealm]外,如何創建特定的領域文件?我從文檔中遺漏了什麼嗎?

+0

喜蒂姆從這裏來的境界。對於那個很抱歉!我不認爲我們已經看到這個錯誤報告。我需要更多的細節:你是在設備上還是在模擬器中崩潰?你還有磁盤空間嗎? – timanglade 2014-09-25 23:25:14

+0

你好,蒂姆,謝謝你的幫助!我正在使用我的設備iPhone 6測試此功能。我有可用的磁盤空間。我嘗試在示例應用中調用[RLMRealm realmWithPath:@「example.realm」],但它會使應用崩潰。我認爲錯誤意味着我必須先創建example.realm文件才能寫入,但我認爲realmWithPath會自動爲您做。 – kevnguy 2014-09-25 23:59:18

+0

我使用0.85.0並獲得通過的CocoaPods – kevnguy 2014-09-26 00:10:49

回答

7

你是正確的,這是創建一個新的境界文件的方式,如果你提供一個完整路徑文件系統的可寫位置,它會工作:

NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; 
NSString *customRealmPath = [documentsDirectory stringByAppendingPathComponent:@"example.realm"]; 
RLMRealm *realm = [RLMRealm realmWithPath:customRealmPath]; 

編輯:已更新至上設備工作的路徑以及模擬器

+0

感謝您的幫助Gusutafu!我在我的設備上運行我的應用程序時嘗試了這一點,但仍然出現了同樣的錯誤。 – kevnguy 2014-09-26 16:51:37

+0

對不起。我看到你在github上尋求幫助,但我更新了答案以供將來參考。 – Gusutafu 2014-09-26 20:35:58

+0

這個答案是正確的。有關更多詳細信息,請參閱https://github.com/realm/realm-cocoa/issues/933。 – jpsim 2014-09-30 23:41:49

0

見我的回答https://github.com/realm/realm-cocoa/issues/4221

如果使用捆綁的境界文件,它會在設備崩潰。只需指定只讀

readOnly:領域是否爲只讀(對於只讀 文件,必須爲true)。

let path = Bundle.main.url(forResource: "mydata", withExtension: "realm")! 
let configuration = Realm.Configuration(fileURL: path, readOnly: true) 
realm = try! Realm(configuration: configuration)