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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…