How can I get the migration to run before the app starts to run the code?

前端 未结 4 1065
野性不改
野性不改 2020-12-17 02:30

I\'m using realm.io in a swift app. This is the first time I\'ve had to run a migration since I have an app in production. I changed one of the models and added a couple of

4条回答
  •  清酒与你
    2020-12-17 02:48

    This works as intended, throwing exceptions when the downgrading (if working with branches where the versions differ) etc. Some sample migrations are shown too.

        Realm.Configuration.defaultConfiguration = Realm.Configuration(
            // Update this number for each change to the schema.
            schemaVersion: 4,
            migrationBlock: { migration, oldSchemaVersion in
                if oldSchemaVersion < 2 {
                    migration.enumerate(SomeObject.className()) { oldObject, newObject in
                        newObject!["newColumn"] = Enum.Unknown.rawValue
                    }
                }
                if oldSchemaVersion < 3 {
                    migration.enumerate(SomeOtherObject.className()) { oldObject, newObject in
                        newObject!["defaultCreationDate"] = NSDate(timeIntervalSince1970: 0)
                    }
                }
                if oldSchemaVersion < 4 {
                    migration.enumerate(SomeObject.className()) { oldObject, newObject in
                        // No-op.
                        // dynamic properties are defaulting the new column to true
                        // but the migration block is still needed
                    }
                }
        })
    

    Obviously that won't compile as is with the SomeObject etc., but you get the idea :)

提交回复
热议问题