views:

8

answers:

1

Hi there,

I did some changes at my CoreData Model. So far, I added a attribute called 'language'. When my application is launched and I click on "Create new Customer" a instance variable Customer is created. This variable is created by:

Customer *newCustomer = [NSEntityDescription insertNewObjectForEntityForName:@"Customer" inManagedObjectContext:appDelegate.managedObjectContext];

Before I did these changes everything worked fine and as planned. But now i get a dump with this error message:reason = "The model used to open the store is incompatible with the one used to create the store";

What do I have to do to solve this? reseting the persistence store didn't help so far.

A: 

What I did to get around this problem was to add this

[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];

to my appDelegate in the persistentStoreCoordinator before adding the persistent store. This deletes the existing store that no longer is compatible with your data model. Remember to comment this line before you run the application the next time if you want to keep what is stored.

My implementation of the persistentStoreCoordinator looks like this when I have to remove an old store.

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

if (persistentStoreCoordinator_ != nil) {
    return persistentStoreCoordinator_;
}
NSError *error = nil;
NSURL *storeURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"MyPinballScore.sqlite"]];
//The following line removes your current store so that you can create a new one that is compatible with your new model
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];

persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}    

return persistentStoreCoordinator_;

}

ehenrik