Edited After Receiving Clarification
Alrighty. So since you do want to pre-populate areas
when you add it to your model, you will need to implement some logic in your migration block after all.
let migrationBlock: MigrationBlock = { migration, oldSchemaVersion in
migration.enumerate(Region.className()) { oldObject, newObject in
if oldSchemaVersion < 1 {
let areas = newObject?["areas"] as? List<MigrationObject>
// Add new objects to 'areas' as needed
}
}
}
There's some sample code showing how to handle List objects in migrations in Realm Swift's sample code collection
If your goal in adding a region
property to Area
is so you can find out which Region
object this Area
is a child of, then you don't need to implement that as a model property. Instead, you can use linkingObjects(_: forProperty: )
to have Realm work that out on your behalf.
class Area: Object {
dynamic var id = 0
dynamic var name = ""
var regions: [Region] {
return linkingObjects(Region.self, forProperty: "areas")
}
override static func primaryKey() -> String? {
return "id"
}
}
To confirm what I said in the comments, migrations are a one-way path. They cannot be downgraded to previous schema versions. If you want to rapidly debug the migration process on a Realm file, I recommend putting the original Realm file aside and working on copies.
Original Answer
Do you actually have any data you wish to add to these new properties? Since it doesn't look like you do, you shouldn't need to implement any code in the migration block.
Simply increase the Realm schema version number, and supply an empty migration block.
let config = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
})
Realm.Configuration.defaultConfiguration = config
While the migration block cannot be nil, you only need to put code in there if there's any data in an old Realm file that you want to manipulate during a migration (i.e., moving it to another property). If you're adding brand new properties, it's not necessary to do anything to them inside the migration block.
It takes a little while to get into the mindset of Realm migrations, but thankfully once you do, you realise they're easier than you thought. :)
(Disclaimer: I work for Realm, but I use it in one of my own shipping iOS apps, where I've played with multiple migrations on real user data at this point. :) )