web-dev-qa-db-ja.com

Swiftでのレルムの移行

そのようにモデル化されたレルムオブジェクトを持っています

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を追加するとエラーが発生する

13
Cody Weaver

マイグレーションを呼び出す必要があります。構成を作成するだけで、呼び出されません。これには2つの方法があります。

  1. 移行で構成をレルムのデフォルト構成として設定します

    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)

これで、移行が正しく機能するはずです。

28
Shripada

作成するのはRealm.Configuration。移行ブロックは、必要に応じてレルムによって呼び出されます。マイグレーションを直接呼び出すことはできません。

したがって、移行ブロックを呼び出すには、構成オブジェクトをレルムに設定するか、デフォルト構成として設定する必要があります。次に、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
4