Core Data Versioning and Migration with Custom Policies

I found the documentation is very limited and my solution to the problem is nowhere.

I need to add a new entity (with relation to an existing one). Also add and rename some attributes of an existing object.

Light weight example:

The old model has one object Itemwith one attribute name.

In the new model, I want to Itemhave one new attribute dateAddedand rename nameto title. In this case, if it dateAddedis optional or given a default value, I could use lightweight migration. Correct me if I am wrong.

But I also want to add a new object Listwith an attribute title. And add to many relationships. The list may be empty or have many elements. An item must refer to only one list.

So, I am confused by what I need to do and what order.

  • Enable migration with the weight transfer function disabled ( NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:NO], NSInferMappingModelAutomaticallyOption, nil];).

  • Create a new version of the model. There I make the changes I want.

  • Create a new mapping model. The source is the old model, the goal is the new model. In ItemToItemI installed titlein $source.name.

, Xcode , , . , List - . NSEntityMigrationPolicy . ?

+5
2

, ...

3 . :

add4. ItemToItemMigrationPolicy, NSEntityMigrationPolicy. :

- (BOOL)beginEntityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
{
    List* list = (List*)[NSEntityDescription insertNewObjectForEntityForName:@"List" inManagedObjectContext:[manager destinationContext]];
    list.name = @"Default list";

    return YES;
}

- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
{
    Item* item = (Item*)[NSEntityDescription insertNewObjectForEntityForName:[mapping destinationEntityName] inManagedObjectContext:[manager destinationContext]];
    item.dateAdded = [NSDate date];
    task.title = [sInstance valueForKey:@"name"];

    [manager associateSourceInstance:sInstance withDestinationInstance:item forEntityMapping:mapping];

    return YES;
}

- (BOOL)createRelationshipsForDestinationInstance:(NSManagedObject *)dInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
{

    NSFetchRequest* fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"List"];
    NSPredicate* predicate = [NSPredicate predicateWithFormat:@"name LIKE 'Default list'"];
    fetchRequest.predicate = predicate;

    NSError* fetchRequestError = nil;
    NSArray* listsArray = [manager.destinationContext executeFetchRequest:fetchRequest error:&fetchRequestError];
    if (fetchRequestError) {
        NSLog(@"%@", fetchRequestError.localizedDescription);
    }
    List* list = [listsArray lastObject];

    ((Item*)dInstance).list = list;

    return YES;
}

ADD5. Xcode ItemToItem ItemToItemMigrationPolicy .

ADD6. () .

ADD7. , item.name . item.title. .

+9

, .

. , . , , .

+1

All Articles