开发者

Dump after some changes in CoreData Model

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.managedObjectConte开发者_StackOverflowxt];

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.


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_;

}


The answer is a bit tricky but this always works for me. This is for a clean installation of a new compatible .sqlite file, not a migration!

launch simulator, delete the app and the data (the popup after you delete the app).

quit simulator

open X-Code, after making any edits to your data model

delete the {*appname*}.sqlite file (or back it up, remove it from project folder, and delete reference)

clean the app (Product > Clean)

Run the app in a simulator (for this tutorial I will assume 4.2)

While the simulator is running, in a Finder window, navigate to: {*home*} > Library > Application Support > iPhone Simulator > 4.2 > Applications > {*random identifier*} > Documents > {*appname*}.sqlite

Copy this file to another location

Stop running your app in X-Code

Drag and drop the {appname}.sqlite file into the files list in X-Code.

In the dialog that pops up, make sure the copy to folder checkbox, is checked.

Product > Clean

Then run the app in the simulator again

Now you should have a working sqlite file!

Cheers, Robert

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜