Swift 中的领域迁移 [英] Realm migrations in Swift

查看:33
本文介绍了Swift 中的领域迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如此建模的领域对象

类 WorkoutSet: Object {//架构 0动态变量练习名称:字符串 = ""动态 var reps: Int = 0//架构 0 + 1动态变量 setCount: Int = 0}

我正在尝试执行迁移.

在我的 AppDelegate 中,我已经导入了 RealmSwift.

在函数 didFinishLaunchWithOptions 我调用

Migrations().checkSchema()

Migrations 是在另一个文件中声明的类.

在该文件中有一个这样声明的结构.

func checkSchema() {领域.配置(//设置新的架构版本.这必须大于以前使用的//版本(如果您之前从未设置过架构版本,则版本为 0).架构版本:1,//设置打开 Realm 时自动调用的块//架构版本低于上面的集合迁移块:{迁移,oldSchemaVersion in//我们还没有迁移任何东西,所以 oldSchemaVersion == 0切换 oldSchemaVersion {情况1:休息默认://没事做!//Realm 会自动检测新属性和移除的属性//并且会自动更新磁盘上的模式self.zeroToOne(迁移)}})}func zeroToOne(迁移:迁移){migration.enumerate(WorkoutSet.className()) {旧对象,新对象让 setCount = 1newObject!["setCount"] = setCount}}

setCount 添加到模型时出现错误

解决方案

您需要调用迁移.仅仅创建一个配置,不会调用它.有两种方法可以做到这一点:

  1. 将您的配置与迁移设置为 Realm 的默认配置-

    让 config = Realm.Configuration(//设置新的架构版本.这必须大于以前使用的//版本(如果您之前从未设置过架构版本,则版本为 0).架构版本:1,//设置打开 Realm 时自动调用的块//架构版本低于上面的集合迁移块:{迁移,oldSchemaVersion in如果 oldSchemaVersion <1 {migration.enumerate(WorkoutSet.className()) { oldObject, newObject innewObject?["setCount"] = setCount}}})Realm.Configuration.defaultConfiguration = 配置

  1. 使用 migrateRealm 手动迁移:

<块引用>

migrateRealm(配置)

现在您的迁移应该可以正常工作了.

I have a Realm Object modeled as so

class WorkoutSet: Object {
     // Schema 0
     dynamic var exerciseName: String = ""
     dynamic var reps: Int = 0
     // Schema 0 + 1
     dynamic var setCount: Int = 0
}

I am trying to perform a migration.

Within my AppDelegate I have imported RealmSwift.

Within the function didFinishLaunchWithOptions I call

Migrations().checkSchema()

Migrations is a class declared in another file.

Within that file there is a struct declared as so.

func checkSchema() {
    Realm.Configuration(
        // Set the new schema version. This must be greater than the previously used
        // version (if you've never set a schema version before, the version is 0).
        schemaVersion: 1,

        // Set the block which will be called automatically when opening a Realm with
        // a schema version lower than the one set above
        migrationBlock: { migration, oldSchemaVersion in
            // We haven’t migrated anything yet, so oldSchemaVersion == 0
            switch oldSchemaVersion {
            case 1:
                break
            default:
                // Nothing to do!
                // Realm will automatically detect new properties and removed properties
                // And will update the schema on disk automatically
                self.zeroToOne(migration)
            }
    })
}

func zeroToOne(migration: Migration) {
    migration.enumerate(WorkoutSet.className()) {
        oldObject, newObject in
        let setCount = 1
        newObject!["setCount"] = setCount
    }
}

I am getting an error for adding setCount to the model

解决方案

You will need to invoke the migration. Merely creating a configuration, won't invoke it. There are two ways of doing this:

  1. Set your configuration with migration as Realm's default configuration-

    let config = Realm.Configuration(
      // Set the new schema version. This must be greater than the previously used
      // version (if you've never set a schema version before, the version is 0).
      schemaVersion: 1,
    
      // Set the block which will be called automatically when opening a Realm with
      // a schema version lower than the one set above
      migrationBlock: { migration, oldSchemaVersion in
    
        if oldSchemaVersion < 1 {
          migration.enumerate(WorkoutSet.className()) { oldObject, newObject in
            newObject?["setCount"] = setCount
          }    
        }
      }
    ) 
    Realm.Configuration.defaultConfiguration = config   
    

OR

  1. Migrate manually using migrateRealm :

migrateRealm(config)

Now your migration should work properly.

这篇关于Swift 中的领域迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆