swift - 在 Swift 中将 deleteRealmIfMigrationNeeded 与 Realm 一起使用时崩溃

标签 swift realm

我在 didFinishLaunchingWithOptions 中执行以下操作:

let config = Realm.Configuration(
  schemaVersion: 0,
  deleteRealmIfMigrationNeeded: true
)
Realm.Configuration.defaultConfiguration = config

let realm = try! Realm()

基本上,在开发过程中,我不想担心迁移,只想在架构更改时清除数据库。我的理解是,这正是 deleteRealmIfMigrationNeeded 的用途。

问题是有时它会在尝试初始化 Realm 时崩溃并出现以下错误:

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=5 "Directory at path '/Users/rock/Library/Developer/CoreSimulator/Devices/D626848E-14D5-47AC-8FFB-9B67D024DEF1/data/Containers/Data/Application/6F71103C-9E10-4131-BED4-D96445FABA52/Documents/default.realm' does not exist."

default.realm 文件正在被删除,大概是因为 deleteRealmIfMigrationNeeded,但是在用最后一行初始化 Realm 时没有重新创建(如我所料)。

有趣的是,如果我手动删除 default.realm.lock 然后重新启动应用程序,它就会工作。

我做错了吗?这可能是一个错误吗? (我使用的是 Realm Swift 2.4.1)

最佳答案

我刚刚遇到了同样的问题,我的解决方案是关闭 Realm 浏览器(如果您打开了它)。

干杯!

关于swift - 在 Swift 中将 deleteRealmIfMigrationNeeded 与 Realm 一起使用时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42099847/

相关文章:

ios - 检查应用程序是否通过 iOS 上的今日小部件启动...?

swift - Collection View 不返回单元格(Swift 3)

ios - 类型不匹配(Swift.Dictionary<Swift.String, Any> debugDescription : "Expected to decode Dictionary<String, Any> but found an array instead."

c# - Xamarin 表单中的 Realm : Another "rhs of the binary operator ' Equal' should be a constant or closure variable expression"issue

ios - Realm 通过其属性查询对象属性字段

iOS:在主从应用程序的导航栏中添加搜索栏

ios - Swift 中的 Alamofire HTTP post 请求

ios - 从 Swift 中的另一个 UIView 中减去 UIView

Android Realm 迭代器异常

swift - 检测 Realm 对象是否符合特定协议(protocol)