Problem with core data migration mapping model
Posted
by dpratt
on Stack Overflow
See other posts from Stack Overflow
or by dpratt
Published on 2009-12-02T02:59:15Z
Indexed on
2010/03/16
19:41 UTC
Read the original article
Hit count: 562
I have an iphone app that uses Core Data to do storage. I have successfully deployed it, and now I'm working on the second version. I've run into a problem with the data model that will require a few very simple data transformations at the time that the persistent store gets upgraded, so I can't just use the default inferred mapping model.
My object model is stored in an .xcdatamodeld bundle, with versions 1.0 and 1.1 next to each other. Version 1.1 is set as the active version. Everything works fine when I use the default migration behavior and set NSInferMappingModelAutomaticallyOption to YES. My sqlite storage gets upgraded from the 1.0 version of the model, and everything is good except for, of course, the few transformations I need done.
As an additional experimental step, I added a new Mapping Model to the core data model bundle, and have made no changes to what xcode generated. When I run my app (with an older version of the data store), I get the following
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Object's persistent store is not reachable from this NSManagedObjectContext's coordinator'
What am I doing wrong? Here's my code for to get the managed object model and the persistent store coordinator.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"gti_store.sqlite"]];
NSError *error;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeUrl
options:options
error:&error]) {
NSLog(@"Eror creating persistent store coodinator - %@", [error localizedDescription]);
}
return _persistentStoreCoordinator;
}
- (NSManagedObjectModel *)managedObjectModel {
if(_managedObjectModel == nil) {
_managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
NSDictionary *entities = [_managedObjectModel entitiesByName];
//add a sort descriptor to the 'Foo' fetched property so that it can have an ordering - you can't add these from the graphical core data modeler
NSEntityDescription *entity = [entities objectForKey:@"Foo"];
NSFetchedPropertyDescription *fetchedProp = [[entity propertiesByName] objectForKey:@"orderedBar"];
NSSortDescriptor* sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES] autorelease];
NSArray* sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[[fetchedProp fetchRequest] setSortDescriptors:sortDescriptors];
}
return _managedObjectModel;
}
© Stack Overflow or respective owner