我不断收到“保存操作失败”的消息 对我的 Xcode 数据模型进行任何更改后

发布于 2024-07-26 02:17:05 字数 2300 浏览 7 评论 0原文

我开始使用 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];
}

这工作得很好,通过按+按钮,新的“我的重复字符串”将被添加到表视图中并保存在持久存储中。

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

但这不起作用! 每次我想添加一行时,模拟器都会崩溃,并且出现以下错误:

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

离笑几人歌 2024-08-02 02:17:06

如果您在模拟器/iPhone 上运行此应用程序,也请卸载该应用程序。 仅在我删除应用程序后才在模拟器上为我工作!

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!

海之角 2024-08-02 02:17:06

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

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

早茶月光 2024-08-02 02:17:06

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

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.

忆梦 2024-08-02 02:17:06

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

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

丢了幸福的猪 2024-08-02 02:17:06

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

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.

月下凄凉 2024-08-02 02:17:05

在应用程序委托中创建 persistenceStoreCoordinator 时,是否设置了 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;
}

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;
}
巴黎盛开的樱花 2024-08-02 02:17:05

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

因此,请转到:~/Library/Application Support/iPhone Simulator/User/Applications/

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文