dbmrq
dbmrq

Reputation: 1461

Use default values for new properties in Realm migration

My Realm data model has a User class and I added a new property to it:

dynamic var useImages: Bool = true

I didn't do anything in the migration block:

if oldSchemaVersion < 1 {
}

Now I expected that after the migration each old User object would have a new useImages property already set to true (the default value). But the useImages property is set to false after the migration.

Is that supposed to happen?

If so, is there an easy way to have Realm use the default values for new properties when migrating databases? Or do I have to do it in the migration block for every new property one by one:

newObject!["useImages"] = true

That seems silly. Is there a better way?

Upvotes: 2

Views: 2747

Answers (1)

Thomas Goyne
Thomas Goyne

Reputation: 8138

This is an unfixed bug, and as you've discovered you have to manually loop over the objects and set the default value in the migration.

Oddly this is currently documented in the Objective-C docs on migrations, but not the Swift docs.

Upvotes: 3

Related Questions