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 NSMigrationManager
can store a userInfo
. 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 initial NSMutableDictionary
and assign it to the NSMigrationManager
's userInfo property.
In createDestinationInstancesForSourceInstance:entityMapping:manager:error:
do the following:
- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
{
NSMutableDictionary *cObjects = (NSMutableDictionary *)[manager userInfo];
NSObject *aUniqueIdentifierForEveryAObject = [sInstance valueForKey:@"aUniqueIdentifier"];
NSManagedObject *destinationC = nil;
if ([[cObjects allKeys] containObject:aUniqueIdentifierForEveryAObject])
{
newC = [userInfo objectForKey:aUniqueIdentifierForEveryAObject];
}
else
{
newC = [NSEntityDescription insertNewObjectForEntityForName:@"C" inManagedObjectContext:[manager destinationContext]];
// Assign instance A (sInstance) to the newC and update the userInfo
[newC setValue:sInstance forKey:@"a"];
[cObjects setObject:newC forKey:aUniqueIdentifierForEveryAObject];
}
[manager associateSourceInstance:sInstance withDestinationInstance:destinationC forEntityMapping:mapping];
return YES;
}
- (BOOL)createRelationshipsForDestinationInstance:(NSManagedObject*)dInstance entityMapping:(NSEntityMapping*)mapping manager:(NSMigrationManager*)manager error:(NSError**)error
{
return YES;
}
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.
- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
{
id destinationB = [NSEntityDescription insertNewObjectForEntityForName:@"B" inManagedObjectContext:[manager destinationContext]];
NSMutableDictionary *userInfo = (NSMutableDictionary *)[manager userInfo];
NSMutableDictionary *cObjects = (NSMutableDictionary *)[userInfo valueForKey:kCObjects];
A *anA = [sInstance valueForKey:@"a"];
NSObject *aUniqueIdentifierForA = [anA valueForKey:@"aUniqueIdentifier"];
C *newC = [userInfo objectForKey:aUniqueIdentifierForA];
[destinationB setValue:newC forKey:@"c"];
// Migrate all other attributes here
[destinationB setValue:... forKey:...];
[manager associateSourceInstance:sInstance destinationB forEntityMapping:mapping];
return YES;
}
- (BOOL)createRelationshipsForDestinationInstance:(NSManagedObject *)dInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
{
return YES;
}
I've not tested this but the basic approach should work.
UPDATE
There was a mistake in Step 1: A-to-C.
// WRONG
[cObjects setObject:newC forKey:sInstance];
// UPDATED
[newC setValue:sInstance forKey:@"a"];
[cObjects setObject:newC forKey:aUniqueIdentifierForEveryAObject];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With