7

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 !

3
  • 3
    I'm in the same boat. The docs for this are terrible, no? Jul 1, 2012 at 7:12
  • yeah ... I wanted to put some bounty on this, but the bounty-link doesn't appear ...
    – Templar
    Jul 2, 2012 at 6:08
  • I deleted that question, it was just an idea of mine.
    – Templar
    Jul 2, 2012 at 9:25

1 Answer 1

9
+50

The basic approach would be

  1. Create a policy for migrating A-to-C
  2. Create a policy for migrating A-to-A
  3. 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];
6
  • Thanks for the reply :) I'll try to implement this right now. Just a short question : How can I be sure that 3. executes AFTER 1. (so I will have a populated userInfo dictionary) ? Is there a migration order in Core Data ?
    – Templar
    Jul 3, 2012 at 10:13
  • You're welcome. In the Mapping Model Editor you can change the order of the Entity Mappings via Drag&Drop. Jul 3, 2012 at 11:41
  • I've implemented it, but it seems it's missing something. More exactly, I can't see where the new C's are associated with the new A instance ? I think I should store the fresh instances of A with the same dictionary approach to associate them with the C's.
    – Templar
    Jul 3, 2012 at 12:31
  • Yeah ... there was an error in the first step. I've updated it. Hope it's now clear what to do. Jul 3, 2012 at 12:53
  • 1
    It's working :D Just a small observation, newC is in destination's context and sIntance in source's, so I've done this in A-to-A. Anyway, thanks a lot and cheers!
    – Templar
    Jul 4, 2012 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.