Realm的setDefaultRealmSchemaVersion未执行

时间:2015-06-17 17:01:14

标签: swift realm

我正在尝试迁移Realm并将属性指定为主键 - 但setDefaultRealmSchemaVersion似乎没有执行:

func applicationDidFinishLaunching(aNotification: NSNotification) {

    // prints "Current Version: 3"
    println("Current version \(schemaVersionAtPath(Realm.defaultPath, encryptionKey: nil, error: nil)!)")

    let migrationBlock: MigrationBlock = { migration, oldSchemaVersion in

        if oldSchemaVersion < 4 {

            migration.enumerate(Fruits.className()) {oldObject, newObject in
                    let id = oldObject!["id"] as! Int
                    newObject!["primaryKeyProperty"] = id
            }
        }
        println("Migration complete")
    }

    setDefaultRealmSchemaVersion(4, migrationBlock)

    // prints "New Version: 3"
    println("New Version: \(schemaVersionAtPath(Realm.defaultPath, encryptionKey: nil, error: nil)!)")  
}

任何想法为什么会这样? 提前谢谢!

1 个答案:

答案 0 :(得分:1)

只有在您明确迁移领域或通过Realm创建领域时,才会执行Realm()的迁移阻止。

相关问题