RLMException, Migration is required for object type RLMException, Migration is required for object type ios ios

RLMException, Migration is required for object type

As long as you have not released your app you can simply delete your app and run it again.

Everytime you change properties on your Realm objects your existing database becomes incompatible to the new one.

As long as you are still in the developing stage you can simply delete the app from the simulator / device and start it again.

Later when your app has been released and you change properties on your objects you have to implement a migration to the new database version.

To actually perform a migration you implement a Realm migration block. Typically you would add the block to application(application:didFinishLaunchingWithOptions:):

var configuration = Realm.Configuration(    schemaVersion: 1,    migrationBlock: { migration, oldSchemaVersion in        if oldSchemaVersion < 1 {            // if just the name of your model's property changed you can do this             migration.renameProperty(onType: NotSureItem.className(), from: "text", to: "title")            // if you want to fill a new property with some values you have to enumerate            // the existing objects and set the new value            migration.enumerateObjects(ofType: NotSureItem.className()) { oldObject, newObject in                let text = oldObject!["text"] as! String                newObject!["textDescription"] = "The title is \(text)"            }            // if you added a new property or removed a property you don't            // have to do anything because Realm automatically detects that        }    })Realm.Configuration.defaultConfiguration = configuration// opening the Realm file now makes sure that the migration is performedlet realm = try! Realm()

Whenever your scheme changes your have to increase the schemaVersion in the migration block and update the needed migration within the block.

Delete the app and re-install is not a good practice. We should incorporate some migration steps during development from the first time we encounter migration need. The link given by SilentDirge is good: realm migration document, which gives good examples for handling different situations.

For a minimum migration task, the following code snippet from the above link can automatically do the migration and is to be used with AppDelegate's disFinishLaunchWithOptions method:

let config = Realm.Configuration(  // Set the new schema version. This must be greater than the previously used  // version (if you've never set a schema version before, the version is 0).  schemaVersion: 1,  // Set the block which will be called automatically when opening a Realm with  // a schema version lower than the one set above  migrationBlock: { migration, oldSchemaVersion in    // We haven’t migrated anything yet, so oldSchemaVersion == 0    if (oldSchemaVersion < 1) {      // Nothing to do!      // Realm will automatically detect new properties and removed properties      // And will update the schema on disk automatically    }  })// Tell Realm to use this new configuration object for the default RealmRealm.Configuration.defaultConfiguration = config// Now that we've told Realm how to handle the schema change, opening the file// will automatically perform the migrationlet _ = try! Realm()