ios - bundle 中包含的 Realm 不会迁移

标签 ios swift3 realm realm-migration

我已经用我当时需要的数据创建了一个 Realm 数据库,但我需要向一个实体添加两个属性。 因此,我只是将这两个属性添加到我的实体中。由于未决的迁移,我预计会发生崩溃,因此我创建了一个额外的迁移。现在,我所做的这些更改不会显示在我的文件系统上的 Realm 数据库中。

我用来迁移的代码如下:

func migrate() {
    let defaultPath = Realm.Configuration.defaultConfiguration.fileURL!
    let realmPath = Bundle.main.url(forResource: "compactedNulTien", withExtension: "realm")

    if let bundledRealm = realmPath {
      do {
        try FileManager.default.removeItem(at: defaultPath)
        try FileManager.default.copyItem(at: bundledRealm, to: defaultPath)
      } catch {}
    }

    let config = Realm.Configuration(schemaVersion: 4, migrationBlock: { migration, oldSchema in
      if oldSchema < 1 {
        migration.enumerateObjects(ofType: PointOfInterest.className(), { (old, new) in
          new!["index"] = 0
        })
      } else if oldSchema < 2 {
        migration.enumerateObjects(ofType: PointOfInterest.className(), { (old, new) in
          new!["place"] = ""
        })
      } else if oldSchema < 3 {
        migration.enumerateObjects(ofType: PointOfInterest.className(), { (old, new) in
          new!["contentImage"] = ""
          new!["contentExtension"] = ""
        })
      }
    })
    Realm.Configuration.defaultConfiguration = config

    do {
      realm = try Realm()
    } catch(let error) {
      Crashlytics().recordError(error)
    }

}

这就是我在我的项目中拥有 Realm 文件的方式,所以我希望那个能够升级。如何在本地 Realm 数据库中获取新属性以便填充它们?

Realm situation

最佳答案

对,我忘记了(可能).realm 被再次复制到模拟器中的 Documents 文件夹中。因此,我在 print(realm.configuration.fileURL ?? "No URL Found") 调用的帮助下从那里选择了它。我再次将其导出为压缩版本,填充它,然后在 Xcode 中进行替换,再次运行应用程序,迁移工作正常,完成。

关于ios - bundle 中包含的 Realm 不会迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45635662/

相关文章:

ios - UITableView 没有显示它应该显示的 Realm 对象

objective-c - xcode 在同一窗口中打开 "click here to view symbol declaration"

ios - 添加背景图像时在 NavigationBar 上显示深色阴影

swift - 无法将类型 'GenericClass<Class>' 的值转换为预期的参数类型 'GenericClass<Class>?'

Android Realm 插入冲突忽略

ios - 在 MongoDB Realm 同步中建模子集合

ios - 用户在 swift 中设置通知时间

iphone - CGColorGetComponents 有问题吗?

ios - 登录到标签栏 Controller 后重定向

ios - 如何以编程方式在 Swift 中使用 UISlider 更改 UIImageView 中指定的图像大小