【问题标题】:Clear db when migration realm needed需要迁移领域时清除数据库
【发布时间】:2015-08-02 10:02:32
【问题描述】:

Android 有一种方法可以实现这一点,但我仍然对接近 iOS 感到困惑。 我不想在每次有新更新时都进行迁移,所以如果是这样,我更喜欢 clear db 重新开始。

我发现此question 与此相关

我只能使用此代码进行迁移吗?

// Notice setSchemaVersion is set to 1, this is always set manually. It must be
// higher than the previous version (oldSchemaVersion) or an RLMException is thrown
[RLMRealm setSchemaVersion:1
        forRealmAtPath:[RLMRealm defaultRealmPath]
    withMigrationBlock:^(RLMMigration *migration, uint64_t oldSchemaVersion) {
   // We haven’t migrated anything yet, so oldSchemaVersion == 0
    if (oldSchemaVersion < 1) {
    // Nothing to do!
    // Realm will automatically detect new properties and removed properties
    // And will update the schema on disk automatically
  }
}];

【问题讨论】:

标签: ios migration database-migration realm realm-migration


【解决方案1】:

是的,您只能将其用于迁移。来自官方文档:

您可以通过调用 +[RLMRealm setSchemaVersion:forRealmAtPath:withMigrationBlock:] 来定义迁移和关联的架构版本。您的迁移块提供了将数据模型从以前的模式转换为新模式的所有逻辑。调用 +[RLMRealm setSchemaVersion:forRealmAtPath:withMigrationBlock:] 后,任何需要迁移的 Realm 都会自动应用提供的迁移块并更新到提供的版本。

Read more about migrations

但是,您可以尝试删除一个领域并在新路径中创建一个新领域。因此,您将在 UserDefaults 中保留一个版本号,并在每次数据模型更改时更新该版本号。我不知道这是否有效。来自文档:

您还可以使用 +[RLMRealm setDefaultRealmPath:] 为默认领域设置自定义路径。这在测试您的应用程序或在 iOS 8 共享容器中的应用程序之间共享领域时特别有用。

Read more about Realm

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 2016-07-20
    相关资源
    最近更新 更多