ios - 您如何进行初始迁移以从非 Realm 持久层迁移?

标签 ios realm

我们目前正在使用 NSUserDefaults 进行数据持久化,我们想开始使用 Realm。但是,由于在添加 Realm 后首次启动时还没有当前 Realm,因此不会运行任何迁移。似乎无法使用 Realm 迁移机制将此“-1”架构迁移到架构 0。

我们想这样做:

// Given that there is no Realm file
// and the app has never run this code before
RLMMigrationBlock migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
    if (oldSchemaVersion < 1) {
        // migrating NSUserDefaults to Realm
    }
};
[RLMRealm setDefaultRealmSchemaVersion:1 withMigrationBlock:migrationBlock];
[RLMRealm defaultRealm]; // this DOESN'T run the migration :(

我们已经考虑过黑客来解决这个问题,但希望那些不是必需的。

最佳答案

解决此问题的最简单方法可能是检查磁盘上是否存在默认域,如果不存在,则访问它(这将创建 is),然后执行 migration手动。

关于ios - 您如何进行初始迁移以从非 Realm 持久层迁移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31795091/

相关文章:

swift - 如何在不使用 do .count 的情况下从结果中一一获取对象

ios - 仅在旋转 ios swift 时在 uiviewcontroller 中制作视频播放器景观

java - RealmTransactions IDE 编辑器错误 : Expected Identifier

ios - 在 subview Controller 中显示导航栏

ios - 在 andom 索引处插入字符串无法快速工作

ios - 境界在今拓

java - 如何在 Android 上的 Realm 中使用构造函数

Java Realm 结果在 Java 7 中排序?

ios - 在 didUpdateFrame 方法中保存 ARSCNView 快照会导致相机卡住

ios - 标签边框未出现(iOS)