【问题标题】:Realm realmWithPath method crashes on iOSRealm realmWithPath 方法在 iOS 上崩溃
【发布时间】:2014-09-25 22:47:11
【问题描述】:

当我打电话时:

[RLMRealm realmWithPath:@"example.realm"]

它崩溃并记录:

由于未捕获的异常“RLMException”而终止应用程序,原因:“open() failed: Operation not allowed”

除了使用 default.realm 和 [RLMRealm defaultRealm] 之外,我如何创建特定的领域文件?我是否遗漏了文档中的某些内容?

【问题讨论】:

  • 你好,来自 Realm 的 Tim。对于那个很抱歉!我认为我们还没有看到这个错误报告。我需要更多细节:您是在设备上还是在模拟器中崩溃?您还有可用的磁盘空间吗?
  • 嗨蒂姆,感谢您帮助我!我正在我的设备 iPhone 6 上对此进行测试。我有可用的磁盘空间。我试图在我的示例应用程序中调用 [RLMRealm realmWithPath:@"example.realm"] 但它会使应用程序崩溃。我认为该错误意味着我必须首先创建 example.realm 文件才能写入它,但我认为 realmWithPath 会自动为您执行此操作。
  • 我正在使用 0.85.0 并通过 CocoaPods 获取框架
  • 它在 group_shared.hpp #ifdef TIGHTDB_EN​​ABLE_REPLICATION inline SharedGroup::SharedGroup(Replication& repl): m_group(Group::shared_tag()), m_transactions_are_pinned(false) { open(repl); } #endif

标签: ios realm


【解决方案1】:

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

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

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

【讨论】:

【解决方案2】:

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

如果您在捆绑中使用领域文件,它将在设备中崩溃。需要指定只读

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多