开发者

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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜