我正在尝试迁移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)!)")
}
任何想法为什么会这样? 提前谢谢!
答案 0 :(得分:1)
只有在您明确迁移领域或通过Realm
创建领域时,才会执行Realm()
的迁移阻止。