在我的 Xcode 数据模型发生任何更改后,我不断收到“保存操作失败”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1091228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:05:35  来源:igfitidea点击:

I keep on getting "save operation failure" after any change on my Xcode Data Model

iphonexcodeversion-controlcore-datamapping-model

提问by Michael Gaylord

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:

我开始使用 Core Data 进行 iPhone 开发。我首先创建了一个非常简单的实体(称为 Evaluation),只有一个字符串属性(称为 EvaluationTopic)。我有以下用于插入新字符串的代码:

- (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:

然后我在 Xcode 中按下“设计 -> 添加模型版本”。我向现有实体添加了三个实体,并向现有的“评估”实体添加了新属性。然后,我通过按“文件 -> 新文件 -> 托管对象类”从实体创建了新文件,并为我的四个实体创建了一个新的 .h 和 .m 文件,包括带有 Evaluation.h 和 Evaluation 的“Evaluation”实体米 现在我通过设置“设计-> 数据模型-> 设置当前版本”来更改模型版本。完成所有这些后,我更改了我的 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.

在更改数据模型上的任何内容后我知道要创建新版本之前,我遇到了这个错误,但为什么这仍然会出现?我需要做任何映射吗(即使我只是添加了以前不存在的实体和属性?)。在 Apple Dev 教程中,这听起来很简单,但我一直在为此苦苦挣扎,在更改模型版本后从未工作过。

回答by hanleyp

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

在 App Delegate 中创建 PersistentStoreCoordinator 时,是否设置了 NSMigratePersistentStoresAutomaticallyOption 和 NSInferMappingModelAutomaticallyOption 选项?

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

回答by Michael Gaylord

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.

如果您只在模拟器中收到此错误,那么您已经更改了数据模型并且它没有删除您之前使用的 sqlite 文件。

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

所以去: ~/Library/Application Support/iPhone Simulator/User/Applications/

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

然后查看以十六进制命名的文件夹,直到看到您的应用程序。打开Documents目录并删除sqlite文件。错误应该消失。

回答by geodude

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!

如果您在模拟器/iphone 上运行它,也请卸载该应用程序。只有在我删除应用程序后才在模拟器上对我来说有效!

回答by elvitucho

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

在模拟器和设备中删除并重新安装应用程序对我有用。

回答by abhinav

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 DELETEthe databse from application folder works for me.

这是由于您的数据库更改,因为在应用程序中有其他数据库,在库/应用程序支持/iPhone 模拟器/用户/应用程序中有其他数据库......所以从应用程序文件夹中删除数据库对我有用。

回答by njyulan

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

您也可以在模拟器中尝试“重置内容和设置...”。这就是对我有用的东西。

回答by 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!

迈克尔的回答符合我的情况。
我修改了核心数据模型并开始收到此错误。
我的解决方案是删除该应用程序(按住 HOME 并穿过该应用程序)并重新启动该应用程序。问题解决了!

回答by Tim Maxey

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

有同样的问题,它曾经工作过,直到我将代码复制到 finder 中的另一个文件夹并开始编辑该项目,开始出现错误。修复它的是我的另一个项目有一个名为 xyz.sqlite 的存储记录器,我正在处理的“新”项目具有相同的名称,不得不将其更改为 xyzv2.sqlite(类似的东西)。在这里找到答案:http: //www.iphonedevsdk.com/forum/iphone-sdk-development/27268-nspersistentstorecoordinator-has-no-persistent-stores.html