enumerate

open fun enumerate(className: String, block: (oldObject: DynamicRealmObject, newObject: DynamicMutableRealmObject?) -> Unit)

Convenience method to iterate all objects of a certain class from the realm before migration with access to an updatable DynamicMutableRealmObject reference to the corresponding object in the already migrated realm. This makes it possible to do more advanced data mapping like merging or splitting field data or moving data while changing the type.

Some common scenarios are shown below:

// Old data model
class MigrationSample: RealmObject {
var firstName: String = "First"
var lastName: String = "Last"
var property: String = "Realm"
var type: Int = 42
}

// New data model
class MigrationSample: RealmObject {
var fullName: String = "First Last"
var renamedProperty: String = "Realm"
var type: String = "42"
}

migrationContext.enumerate("MigrationSample") { oldObject: DynamicRealmObject, newObject: DynamicMutableRealmObject? ->
newObject?.run {
// Merge property
set( "fullName", "${oldObject.getValue<String>("firstName")} ${ oldObject.getValue<String>("lastName") }" )

// Rename property
set("renamedProperty", oldObject.getValue<String>("property"))

// Change type
set("type", oldObject.getValue<Long>("type").toString())
}
}

Parameters

className

the name of the class for which to iterate all instances in the old realm.

block

block of code that will be triggered for each instance of the class in the old realm. The newObject will be a reference to the corresponding DynamicMutableRealmObject in the already migrated realm, or null if the object has been deleted.