NSInternalInconsistencyException: This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation

In: Tech

14 Jan 2010

I got this error last night after going through the Core Data Tutorial on the Apple dev site. I completed the tutorial and everything worked fine… then I added a few more things to the UI and a new field to the Event Managed Object. When I tried to re-build and run, I got that error above (“NSInternalInconsistencyException: This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation”) when trying to save an Event.

After doing some research, it turns out that I need to explicitly tell NSPersistentStoreCoordinator to automatically migrate newer versions of the data objects. I had to set these 2 values and pass them into the NSPersistentStoreCoordinator as options when it is created:

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 if failure
}

I’ll update this post as I understand more about what is going on…

Comment Form