Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

objective c - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...