The basic approach would be
- Create a policy for migrating A-to-C
- Create a policy for migrating A-to-A
- Create a policy for migrating B-to-B
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];