简体   繁体   English

本地领域迁移无法正常工作

[英]Local Realm migration not working Swift

I don't know if I'm missing something here. 我不知道我是否在这里缺少任何东西。

I got a crash after running my app without a migration block after updating a property (same problem as here Realm Migration not working ) 更新属性后,在运行我的应用程序而没有迁移块的情况下发生崩溃(与这里的Realm Migration无法正常工作一样

But now when I run the app, it must run the migration because it no longer crashes but my object's properties are not updated. 但是现在,当我运行该应用程序时,它必须运行迁移,因为它不再崩溃,但我的对象的属性未更新。

I have updated the below object ("minReps" is the one I've added): 我已经更新了以下对象(“ minReps”是我添加的对象):

class ExerciseGeneratorObject: Object {
    @objc dynamic var name = ""
    @objc dynamic var minReps = 0
    @objc dynamic var maxReps = 0

    convenience init(name: String, minReps: Int, maxReps: Int) {
        self.init()
        self.name = name
        self.minReps = minReps
        self.maxReps = maxReps
    }

Then I'm running an empty migration block like this in appDelegate : 然后我在appDelegate中运行一个空的迁移块:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let config = Realm.Configuration(

        schemaVersion: 3,

        migrationBlock: { migration, oldSchemaVersion in

            if (oldSchemaVersion < 3) {

            }
    })

    Realm.Configuration.defaultConfiguration = config

    let realm = try! Realm()

I thought Realm was meant to update object properties automatically if you run an empty migration block - is that wrong? 我认为,如果您运行一个空的迁移块,则Realm旨在自动更新对象属性-是吗? Am I missing some code to make this work? 我是否缺少一些代码来完成这项工作?

There's a very similar problem here ( Realm migrations in Swift ) (the isn't me!) but looks out of date now (and sure I've tried the solutions there as above!) 这里有一个非常相似的问题( 在Swift中进行Realm迁移 )(不是我!),但现在看起来已经过时了(并确保我已经尝试过上述解决方案!)

Current schema version should be set in the app via realm configuration, and you should increase it, in your code you set schema version to 3, and asking realm to migrate realm if oldSchemaVersion less than 3, set schema version to 4, and it will work 当前模式版本应通过领域配置在应用程序中设置,并应增加它,在代码中将模式版本设置为3,如果oldSchemaVersion小于3,则要求领域迁移领域,将模式版本设置为4,它将工作

    var 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: 4,

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

            if (oldSchemaVersion < 3) {
                // Nothing to do!
                // Realm will automatically detect new properties and removed properties
                // And will update the schema on disk automatically
            }
    })

    Realm.Configuration.defaultConfiguration = config
    config = Realm.Configuration()
    config.deleteRealmIfMigrationNeeded = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM