领域迁移记录不充分。谁能澄清一下吗?

Adr*_*the 3 realm ios

在 Realm 数据库上执行迁移的记录很少,并且文档似乎已经过时。有两个方面解释如何迁移数据:

-- Realm 网站上的简单示例: https ://realm.io/docs/swift/latest/

-- Github 示例部分中有更详细的示例: https://github.com/realm/realm-cocoa/blob/master/examples/ios/swift-3.0/Migration/AppDelegate.swift

这些示例都没有充分解释如何在架构版本之间迁移数据。我尝试过使用这些示例,但尚未实现任何迁移。此外,在升级到较新的 Realm 版本而不进行架构更改和数据更改时,我遇到了应用程序崩溃的问题,这种情况不会在模拟器中发生,但会在从 TestFlight 或 App Store 安装应用程序时发生。

似乎详细说明迁移的领域文档和示例需要刷新。我感兴趣的领域是:

  1. 升级到较新的 Realm 版本,无需更改数据库中的架构。不清楚我是否应该继续使用以前版本生成的 default.realm 文件,或者是否需要使用较新的 Realm 框架版本重新生成 default.realm 文件。

  2. 向 Realm 对象添加新属性。

  3. 添加到现有类的新对象(“行”)无需任何架构更改。

  4. 数据库中现有类的架构没有更改,但添加了一个或多个全新的类。

  5. 上述任意组合。

谢谢!

Ada*_*ish 5

抱歉,文档还不够。我们感谢您的反馈,并将利用它来改进它们。同时,我来回答一下大家的问题:

\n\n
    \n
  1. 升级SDK时无需执行任何操作。有时,我们会升级核心数据库文件格式,但这种迁移会在您打开 Realm ( Realm()) 时自动发生,因此您不必担心。
  2. \n
  3. 当您向对象添加新属性时,您只需遵循此代码片段即可。
  4. \n
\n\n

迁移块中不需要任何内容​​,因为该块只是在版本之间应用数据转换。您需要做的就是增加schemaVersion

\n\n
// Inside your application(application:didFinishLaunchingWithOptions:)\n\nlet config = Realm.Configuration(\n  // Set the new schema version. This must be greater than the previously used\n  // version (if you\'ve never set a schema version before, the version is 0).\n  schemaVersion: 1,\n\n  // Set the block which will be called automatically when opening a Realm with\n  // a schema version lower than the one set above\n  migrationBlock: { migration, oldSchemaVersion in\n    // We haven\xe2\x80\x99t migrated anything yet, so oldSchemaVersion == 0\n    if (oldSchemaVersion < 1) {\n      // Nothing to do!\n      // Realm will automatically detect new properties and removed properties\n      // And will update the schema on disk automatically\n    }\n  })\n\n// Tell Realm to use this new configuration object for the default Realm\nRealm.Configuration.defaultConfiguration = config\n\n// Now that we\'ve told Realm how to handle the schema change, opening the file\n// will automatically perform the migration\nlet realm = try! Realm()\n
Run Code Online (Sandbox Code Playgroud)\n\n
    \n
  1. 将对象添加到领域不会影响架构,因此迁移不相关。
  2. \n
  3. 这与 2 相同,您只需增加 ,schemaVersion但无需在迁移块中执行任何操作,因为 Realm 会处理所有事情。迁移块用于自定义迁移逻辑,例如,您希望在更新到 时将firstNamelastNamefrom转换schemaVersion=0为。在这种情况下,您可以从旧版本获取数据并将字符串连接到新版本中fullNameschemaVersion=1fullName迁移块内的新属性中。
  4. \n
\n\n

希望这可以帮助!

\n