我不断收到“保存操作失败"的消息在我的 Xcode 数据模型发生任何变化之后 [英] I keep on getting "save operation failure" after any change on my Xcode Data Model

查看:12
本文介绍了我不断收到“保存操作失败"的消息在我的 Xcode 数据模型发生任何变化之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.'"

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

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.

推荐答案

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

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

这篇关于我不断收到“保存操作失败"的消息在我的 Xcode 数据模型发生任何变化之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆