加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在Swift中使用带有Realm的deleteRealmIfMigrationNeeded时崩溃

发布时间:2020-12-14 04:46:51 所属栏目:百科 来源:网络整理
导读:我在didFinishLaunchingWithOptions中执行以下操作: let config = Realm.Configuration( schemaVersion: 0,deleteRealmIfMigrationNeeded: true)Realm.Configuration.defaultConfiguration = configlet realm = try! 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浏览器,如果你打开它.

干杯!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读