swift3 - Realm Migration: Move an Object from one Object to another Object -
i have 3 objects:
class customer: object { dynamic var solution: solution!; ... } class solution: object { dynamic var data: data!; ... } class data: object { ... }
now need move data
object solution
customer
becomes:
class customer: object { dynamic var solution: solution!; dynamic var data: data!; ... }
i have no idea how have implement realm migration method works fine , wont lose data.
i did experiments realm migrations sample app , came potential solution:
in migration block, can interact realm file via migration
object. attempts directly access realm file mid-migration result in exception.
that being said, it's possible have nested calls migration.enumerateobjects
referencing different realm model object classes. such, should matter of enumerating through customer
objects, , in each iteration, enumerate through solution
objects find corresponding 1 right data
value. once found, should possible set customer
object data solution
object.
realm.configuration.defaultconfiguration = realm.configuration( schemaversion: 1, migrationblock: { migration, oldschemaversion in if (oldschemaversion < 1) { migration.enumerateobjects(oftype: customer.classname()) { oldcustomerobject, newcustomerobject in migration.enumerateobjects(oftype: solution.classname()) { oldsolutionobject, newsolutionobject in //check solution object 1 referenced customer guard oldcustomerobject["solution"].isequal(oldsolutionobject) else { return } //copy data newcustomerobject["data"] = oldsolutionobject["data"] } } } } })
i feel need stress code no means tested , guaranteed work in present state. recommend make sure thoroughly test on dummy data wouldn't miss beforehand. :)
Comments
Post a Comment