Jul 15, 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])
{
    // Handle error
    NSLog(@"error %@, %@", error, [error userInfo]);
}

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?

Share this Post


                           

Comments


Turkse Community     Mar 16, 2010
@ Toby Joe Boudreaux

Thanks for the mapping models. Very usefull.

Cole Joplin     Jan 02, 2010
I did some experiments, and I came to find that blowing away the iPhone Sim files was not the answer. I could take a project, have it working perfectly. The I add a new entity in the model, no code even touching the new entity, and it crashes when accessing the original entity. I've looked around a lot at solutions, even turning on migrations, and wasted a lot of time. It seems that the only truly safe thing that works, at least in 3.1.2, is to make sure your data model is done, and won't need any modification, before you continue building the rest of the project.

Doc_Lancers     Nov 26, 2009
I followed that tutorial on core Date and got similar error response. How do you delete that .sqlite file? could not find it. Also did anyone find a better way?

Dale Bradshaw     Jul 24, 2009
mogenerator is super handy for Core Data model changes on OSX. I haven't experimented with it on the Iphone, but might be worth looking into.

http://rentzsch.com/code/mogenerator


Evan Cordes     Jul 16, 2009
There's a great library available for persisting data on the iPhone that I've used when building quick prototypes.

http://code.google.com/p/sqlitepersistentobjects/


Patrick Hefner     Jul 15, 2009
Why not interface with SQLite directly? I found this to be one of the most useful features of obj-c after moving to iPhone Dev from Web Dev. There are a bunch of great SQLite wrappers out there as well. One of the easiest to use was created by Bill Dudney, its open source and I believe its introduced
with the pragprog iPhone screen casts.

Toby Joe Boudreaux     Jul 15, 2009
Yep. You can use a mapping model to migrate to new schemas, but for dev, it's often faster to just blow away the file store. Pay attention when writing unit tests, too.

More on mapping models: http://devworld.apple.com/documentation/Cocoa/ Conceptual/CoreDataVersioning/Introduction/Introduction.html

James Hall     Jul 15, 2009
Thanks for this!

I havent gotten to work too deeply with core data yet, but have been looking at it. It looks like a beast!

Please share any other gotchas you may run into!


Speak






Submit »