I keep on getting “save operation failure” after any change on my XCode Data Model

我们两清 提交于 2019-11-28 03:42:59

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

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.

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!

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

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.

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

Stove

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!

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!