22

I started using Core Data for iPhone development. I started out by creating a very simple entity (called Evaluation) with just one string property (called evaluationTopic). I had following code for inserting a fresh string:

- (void)insertNewObject {

    // Create a new instance of the entity managed by the fetched results controller.
    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[fetchedResultsController fetchRequest] entity];
    NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

    // If appropriate, configure the new managed object.
    [newManagedObject setValue:@"My Repeating String" forKey:@"evaluationTopic"];

    // Save the context.
    NSError *error;
    if (![context save:&error]) {
        // Handle the error...
    }

    [self.tableView reloadData];
}

This worked perfectly fine and by pushing the +button a new "My Repeating String" would be added to the table view and be in persistent store.

I then pressed "Design -> Add Model Version" in Xcode. I added three entities to the existing entity and also added new properties to the existing "Evaluation" entity. Then, I created new files off the entities by pressing "File -> New File -> Managed Object Classes" and created a new .h and .m file for my four entities, including the "Evaluation" entity with Evaluation.h and Evaluation.m. Now I changed the model version by setting "Design -> Data Model -> Set Current Version". After having done all this, I changed my insertMethod:

- (void)insertNewObject {

    // Create a new instance of the entity managed by the fetched results controller.
    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[fetchedResultsController fetchRequest] entity];
    Evaluation *evaluation = (Evaluation *) [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

    // If appropriate, configure the new managed object.
    [evaluation setValue:@"My even new string" forKey:@"evaluationSpeechTopic"];

    // Save the context.
    NSError *error;
    if (![context save:&error]) {
        // Handle the error...
    }

    [self.tableView reloadData];
}

This does not work though! Every time I want to add a row the simulator crashes and I get the following:

"NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores.  It cannot perform a save operation.'"

I had this error before I knew about creating new version after changing anything on the datamodel, but why is this still coming up? Do I need to do any mapping (even though I just added entities and properties that did not exist before?). In the Apple Dev tutorial it sounds very easy but I have been struggling with this for long time, never worked after changing model version.

Cœur
  • 37,241
  • 25
  • 195
  • 267

8 Answers8

50

Do you have NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption options set when you create your persistentStoreCoordinator in the App Delegate?

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"database.sqlite"]];

    NSError *error = nil;
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
        // Handle error
    }    

    return persistentStoreCoordinator;
}
hanleyp
  • 811
  • 9
  • 5
  • You're a genius. Thanks you soo much, this solved my problem. Any new versions in data model is now accepted for saving. –  Jul 07 '09 at 21:16
  • 2
    Note to self and others who tried this: unlike NSMigratePersistentStoresAutomaticallyOption, which is part of the CoreData.framework, NSInferMappingModelAutomaticallyOption belongs to the SyncServices.framework. Google on "Syncing Core Data Applications" for more information. – Elise van Looij Oct 04 '09 at 16:33
  • 2
    this answer is not complete. Is persistentStoreCoordinator a property in the App Delegate? Do you have to use this method at all later? – coolcool1994 Jul 01 '13 at 18:49
  • @ElisevanLooij I found it in the CoreData.h file. – csotiriou Jan 19 '15 at 13:34
30

If you are only getting this error in the Simulator then you have changed your data model and it hasn't deleted the sqlite file that you were previously using.

So go to: ~/Library/Application Support/iPhone Simulator/User/Applications/

Then look through the HEX-named folders until you see your app. Open the Documents directory and delete the sqlite file. The error should go away.

Michael Gaylord
  • 7,282
  • 8
  • 50
  • 47
  • 1
    This worked for me. Mine was in ~/Library/Application Support/iPhone Simulator/4.2/Applications/XXXXXXX-XXX.../Documents. – James Zaghini Feb 05 '11 at 14:05
  • I am not able to locate iPhone Simulator folder inside Application Support. How come? – Varundroid Oct 30 '11 at 08:32
  • You probably can't see your Application Support folder because your Library folder is hidden as it's hidden by default in Mac OSX Lion. In Finder press Cmd+Shift+G then type ~/Library/Application Support/iPhone Simulator/ – Michael Gaylord Mar 11 '12 at 15:52
  • 1
    Excellent - thanks. As an update, the simulators have moved under Xcode 6 / iOS8 to Library/Developer/CoreSimulator/Devices and all have coded names - the same solution still works though once you find the sqlite files. – Ali Beadle Apr 17 '15 at 17:47
10

if you are running this on the simulator/iphone, also uninstall the app too. worked for me on the simulator only after i deleted the app!

geodude
  • 101
  • 1
  • 2
4

Deleting and re-installing the app in both, simulator and device, worked for me.

elvitucho
  • 126
  • 6
3

this is due to your database change because in app there is other database and in Library/Application Support/iPhone Simulator/User/Applications there is other database ....so DELETE the databse from application folder works for me.

abhinav
  • 312
  • 1
  • 11
0

Michael's answer fit my case.
I modified the core data model and starts getting this error.
My solution was remove the app(hold HOME and CROSS the app) and relunch the app. Problem solved!

The iOSDev
  • 5,237
  • 7
  • 41
  • 78
Stove
  • 116
  • 4
0

You could also try "Reset Content and Settings..." in the Simulator. That's what worked for me.

njyulan
  • 43
  • 8
0

Had same issue and it use to work, until I copied the code to another folder in finder and started editing that project, starting getting the error. What fixed it was my other project had a storecordinator with name xyz.sqlite, the "new" project I was working on had same name, had to change it to xyzv2.sqlite (something like that). Found answer here: http://www.iphonedevsdk.com/forum/iphone-sdk-development/27268-nspersistentstorecoordinator-has-no-persistent-stores.html

Tim Maxey
  • 749
  • 6
  • 13