Swift中的Realm迁移

tvz2xvvm  于 2023-04-10  发布在  Swift
关注(0)|答案(3)|浏览(139)

我有一个领域对象建模为

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

我正在尝试执行迁移。
在我的AppDelegate中,我导入了RealmSwift
在函数didFinishLaunchWithOptions中,我调用

Migrations().checkSchema()
  • Migrations* 是在另一个文件中声明的类。

在该文件中有一个结构体声明为。

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
    }
}

setCount添加到模型时出现错误

kx7yvsdv

kx7yvsdv1#

您将需要调用迁移。仅创建配置不会调用它。有两种方法可以执行此操作:
1.将迁移配置设置为Realm的默认配置-

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

1.使用migrateRealm手动迁移:
migrateRealm(config)
现在您的迁移应该可以正常工作了。

vlju58qv

vlju58qv2#

因为您只是创建了Realm.Configuration,迁移块是由Realm根据需要调用的,不能直接调用迁移。
所以要成为调用迁移块,需要将配置对象设置为Realm,或者设置为默认配置,然后创建Realm示例。
因此,您需要执行以下操作:

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
        // 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)
        }
})

let realm = try! Realm(configuration: config) // Invoke migration block if needed
34gzjxbg

34gzjxbg3#

更新Xcode 14.3的回答@kishikawa katsumi:

func zeroToOne(migration: Migration) {
    migration.enumerateObjects(ofType: <Your Realm Object>.className()) { _, newObject in
        newObject![<Your new properties>] = nil
        // Ex: newObject!["release_date"] = nil
    }
}

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

        // 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
            zeroToOne(migration: migration)
        }
    }
)

let realm = try! Realm(configuration: config) // Invoke migration block if needed

相关问题