Swift Realm Property '*' has been added to latest object model MIGRATION

我怕爱的太早我们不能终老 提交于 2019-11-28 03:25:35

问题


I have added new array attribute to the RLMObject and

public class Student: RLMObject {
    dynamic var id = 0
    dynamic var name = ""
    dynamic var resultList = RLMArray(objectClassName:Result.className())
}

public class Result: RLMObject {
}

ERROR Log:

Migration is required for object type 'Student' due to the following errors: - Property 'resultList' has been added to latest object model.

TRY Failed:

let configuration:RLMRealmConfiguration = RLMRealmConfiguration.defaultConfiguration()

migration.enumerateObjects(Student.className()) { oldObject, newObject in
    newObject!["resultList"] = RLMArray(objectClassName: Result.className())
}

EDIT:

  let configuration:RLMRealmConfiguration = RLMRealmConfiguration.defaultConfiguration()
    print("Realm db current version: \(configuration.schemaVersion)")
    configuration.schemaVersion = 1
    configuration.migrationBlock = {(migration:RLMMigration, oldSchemaVersion: UInt64) in
        print("Realm db migration start")
        if oldSchemaVersion < 1 {
            print("Schema version: 1 - Rename fields")
            migration.enumerateObjects(Student.className()) { oldObject, newObject in
                newObject!["creationDate"] = oldObject!["createdAt"]
                newObject!["modifiedDate"] = oldObject!["updatedAt"]
            }
        }
        print("Realm db migration finish")
    }
    RLMRealmConfiguration.setDefaultConfiguration(configuration)
    let realm = RLMRealm.defaultRealm()

SOLUTION:

update your version to +1

configuration.schemaVersion += 1

回答1:


You have to incremented your schemaVersion and provide a migrationBlock on your RLMRealmConfiguration. In there you can migrate tables. But you don't need that in your specific case here. The addition of properties can be handled automatically. You'll still need an empty block.

let config = RLMRealmConfiguration.defaultConfiguration()
config.schemaVersion = 1
config.migrationBlock = { (migration, oldSchemaVersion) in
    // nothing to do
}
RLMRealmConfiguration.setDefaultConfiguration(config)


来源:https://stackoverflow.com/questions/34277061/swift-realm-property-has-been-added-to-latest-object-model-migration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!