I have the following models
class Area: Object {
// Specify properties to ignore (Realm won't persist these)
// override static func ignoredProperties() -> [String] {
// return []
// }
dynamic var id = 0
dynamic var name = ""
override static func primaryKey() -> String? {
return "id"
}
}
class Region: Object {
// Specify properties to ignore (Realm won't persist these)
// override static func ignoredProperties() -> [String] {
// return []
// }
dynamic var id = 0
dynamic var name = ""
override static func primaryKey() -> String? {
return "id"
}
}
And I would like to add let areas = List<Area>()
to Region class and dynamic var region: Region?
to Area class, may I ask how to perform the migration block? Cause the example in the migration documentation is only demo-ing the primitive types.
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.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 toArea
is so you can find out whichRegion
object thisArea
is a child of, then you don't need to implement that as a model property. Instead, you can uselinkingObjects(_: forProperty: )
to have Realm work that out on your behalf.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.
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. :) )
Here is a possible solution imagining that older schema version was 0, and new one is 1:
Note: You will need to implement the methods that return you area for a given region and vice versa.