I have an object NotSureItem
in which I have three properties title
whose name is renamed from text
and textDescription
which I had added later and a dateTime
property. Now when I am going to run my app it crashes when I want to add something to these properties. It shows following statements.
'Migration is required for object type 'NotSureItem' due to the following errors:
- Property 'text' is missing from latest object model.
- Property 'title' has been added to latest object model.
- Property 'textDescription' has been added to latest object model.'
Here is my code:
import Foundation
import Realm
class NotSureItem: RLMObject {
dynamic var title = "" // renamed from 'text'
dynamic var textDescription = "" // added afterwards
dynamic var dateTime = NSDate()
}
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 performed
let 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 Realm
Realm.Configuration.defaultConfiguration = config
// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
let _ = try! Realm()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With