July 15th, 2009

iPhone: CoreData Error

In an attempt to learn about CoreData to maintain persistent data on the iPhone I followed this tutorial. (I recommend following it and then opening up some of the provided examples from Apple that illustrate the use of CoreData.) Indeed it has a good deal of descriptions on how it all fits together and gave me great insight on how to implement a persistent Model.

However, once I completed the tutorial to a point where I was able to build and run, the app was crashing and I was perplexed as to why. So I started with what the Debugger was telling me.

2009-07-13 13:08:48.605 Locations[57726:20b] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘This NSPersistentStoreCoordinator has no persistent stores.  It cannot perform a save operation.’

First I decided to add a trace to the method that creates the persistent store coordinator for the application:

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error])
<br />{
<br />    // Handle error
<br />    NSLog(@"error %@, %@", error, [error userInfo]);
<br />}

I built the app once again and the error was printed quite nicely. The thing that caught my eye was: reason = “The model used to open the store is incompatible with the one used to create the store”;

A quick look around the net yielded a few results which led me to look for the .sqlite file that the iPhone Simulator creates for the app … and delete it.

Yes, it appears that when you are creating new entities in your Data Model (at least initially) and adding attributes and/or relationships, XCode doesn’t agree with things. The solution is to simply delete the .sqlite file and build again. The only question at this point is: will this happen each time I add a new attribute or another entity?