在 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 安装应用程序时发生。
似乎详细说明迁移的领域文档和示例需要刷新。我感兴趣的领域是:
升级到较新的 Realm 版本,无需更改数据库中的架构。不清楚我是否应该继续使用以前版本生成的 default.realm 文件,或者是否需要使用较新的 Realm 框架版本重新生成 default.realm 文件。
向 Realm 对象添加新属性。
添加到现有类的新对象(“行”)无需任何架构更改。
数据库中现有类的架构没有更改,但添加了一个或多个全新的类。
上述任意组合。
谢谢!
抱歉,文档还不够。我们感谢您的反馈,并将利用它来改进它们。同时,我来回答一下大家的问题:
\n\nRealm()) 时自动发生,因此您不必担心。迁移块中不需要任何内容,因为该块只是在版本之间应用数据转换。您需要做的就是增加schemaVersion
// 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()\nRun Code Online (Sandbox Code Playgroud)\n\nschemaVersion但无需在迁移块中执行任何操作,因为 Realm 会处理所有事情。迁移块用于自定义迁移逻辑,例如,您希望在更新到 时将firstName和lastNamefrom转换schemaVersion=0为。在这种情况下,您可以从旧版本获取数据并将字符串连接到新版本中fullNameschemaVersion=1fullName迁移块内的新属性中。希望这可以帮助!
\n| 归档时间: |
|
| 查看次数: |
740 次 |
| 最近记录: |