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

前端 未结 4 1058
野性不改
野性不改 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 03:11

    I found this to work best in a View Controller:

    let migrationBlock: MigrationBlock = { migration, oldSchemaVersion in
        //Leave the block empty
    }
    
    lazy var realm:Realm = {
        Realm.Configuration.defaultConfiguration = Realm.Configuration(schemaVersion: 1, migrationBlock: migrationBlock)
        return try! Realm()
    }()
    

    In other words, move the configuration assignment into the block so it doesn't get executed too late.

提交回复
热议问题