Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data versioning and migrating with custom policy

I've found documentation very limited and my problem solution nowhere.

I need to add new entity (with relationship to existing one). Also add and rename some of attributes of existing entity.

Lightweighted example:

Old model has one entity Item with one attribute name.

In new model I want to Item to have one new attribute dateAdded and rename name to title. At this point, if dateAdded would be optional or given default value, I could use lightweight migration feature. Correct me if I am wrong.

But I also want to add new List entity with title attribute. And add to-many relationship. List can be empty or have many items. Item have to refer to exactly one list.

So I am confused in what everything I have to do and what order.

  1. Turn on migration with lightweighted migration feature disabled (NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:NO], NSInferMappingModelAutomaticallyOption, nil];).

  2. Create a new version of model. There I do changes I want to.

  3. Create a new mapping model. Source is old model, target is new model. In ItemToItem I set title to $source.name.

Besides Xcode is still crashing on working with mapping model I do not know what to do next. I think I have to create one instance of List in context and make all items referring to it because of relationship policy. And I thing I should use custom NSEntityMigrationPolicy to do this. Any help to accomplish this challenge?

like image 532
user500 Avatar asked Aug 11 '11 13:08

user500


People also ask

What is core data migration?

Core Data can typically perform an automatic data migration, referred to as lightweight migration. Lightweight migration infers the migration from the differences between the source and the destination managed object models.

What is heavy weight migration IOS?

Use heavyweight (manual) migration in rare cases when changes to the data model exceed the capabilities of lightweight migration.

What is core data in Xcode?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.


2 Answers

Well, I did it...

My first 3 steps were correct. Continue scenario:

ADD4. Do an ItemToItemMigrationPolicy, a subclass of NSEntityMigrationPolicy. Override:

- (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. In Xcode in mapping model set ItemToItem migration policy to custom with ItemToItemMigrationPolicy value.

ADD6. Make your new model version current and generate (replace) classes from new or changed entities.

ADD7. Do changes in your code, for example item.name no more works. Now it is item.title. Clean project and run.

like image 181
user500 Avatar answered Sep 24 '22 06:09

user500


If you are adding a new entity, then you will need to use a custom mapping model and turn off lightweight migration.

One important thing. When working with migration, make sure you start with a fresh example of the existing persistent store every time especially if you have had crashes. If you don't, you can corrupt the store which will cause errors to snowball.

like image 33
TechZen Avatar answered Sep 25 '22 06:09

TechZen