I’m trying to make a database upgrade using a custom NSEntityMigrationPolicy. This is how my data model looks like now and what is the intended result :
A <-------->> B ( 1 A -> many B )
I want a new C entity which will “break” the B type objects in some subsets :
A <-------->> C <------->> B ( 1 A can have many C, each C can have many B )
I made a custom NSEntityMigrationPolicy for entity A. I created some C entities for each A in createDestinationInstancesForSourceInstance:entityMapping:manager:error: , but I’m stuck what to do with the relation between A and B.
In docs they say to use createRelationshipsForDestinationInstance somehow to create the new relations. I haven’t found any suggestive examples however for this case. I suppose probably the A <->> C relation can be created here, but the B’s may not exist yet in the destination context to create a C<–>>B relation yet. How can a migration like this be done? Please, help 🙂
Thanks !
The basic approach would be
It’s important to do it in the correct order.
As C does not exist in the database you have to do a little trick: The
NSMigrationManagercan store auserInfo. This userInfo dictionary is available throughout the whole migration process. Therefore you can store every C object you’ve created in A-to-C in the userInfo dict. You need to have a unique identifier attribute on A to store this as the key and set the appropriate C object as the value. In the third migration (B-to-C) you can then access the appropriate C object and assign it to B.1. A-to-C: Creates the new C objects based on every existing A object.
In
beginEntityMapping:manager:error:create the your initialNSMutableDictionaryand assign it to theNSMigrationManager‘s userInfo property.In
createDestinationInstancesForSourceInstance:entityMapping:manager:error:do the following:2. A-to-A: Migrates all A objects
Here you can do some additional adjustments to A’s attributes. It’s important, that you create this migration whether you have attribute changes or not, as automatic migration is turned off. If you do not have any changes, you can simply create an Entity Mapping in the mapping model editor.
3. B-to-B: Migrates all B objects and assigns new C objects
Assign the new C objects. You cannot to this in
createRelationships...as you need the A object and it’s unique identifier to get the appropriate C from the userInfo dict.I’ve not tested this but the basic approach should work.
UPDATE
There was a mistake in Step 1: A-to-C.