Realm realmWithPath方法在iOS上崩溃

时间:2014-09-25 22:47:11

标签: ios realm

我打电话的时候:

[RLMRealm realmWithPath:@"example.realm"]

它崩溃并记录:

由于未捕获的异常“RLMException”而终止应用程序,原因:'open()失败:操作不被允许'

除了使用default.realm和[RLMRealm defaultRealm]之外,如何创建特定的域文件?我错过了文档中的内容吗?

2 个答案:

答案 0 :(得分:7)

您是对的,这是创建新域文件的方法,如果您提供文件系统中可写位置的完整路径,它将起作用:

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

编辑:更新为适用于设备和模拟器的路径

答案 1 :(得分:0)

请参阅我的回答https://github.com/realm/realm-cocoa/issues/4221

如果您在bundle中使用realm文件,它将在设备中崩溃。需要指定只读

  

readOnly:Realm是否为只读(对于只读,必须为true)   文件)。

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