Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 209
AJit Avatar asked Dec 14 '15 21:12

AJit


1 Answers

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)
like image 115
marius Avatar answered Nov 15 '22 18:11

marius