ios - 还有一个原因 : '+entityForName: could not locate an NSManagedObjectModel for entity name

标签 ios core-data nsmanagedobjectcontext detailsview master-detail

我有以下错误:

2012-04-18 10:15:49.585 FoodXJournal[13931:fb03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'CommodityTypes''

我已经阅读了这个主题的大部分其他问题,并做了一些检查。

我有一个包含静态 TableView 的详细 View Controller 。表格 View 的一个单元格标记为“删除”,并链接到名为“deleteCT”的segue。我希望应用删除 self.detailItem并在点击此单元格时转到主视图 Controller 。这是我的方法。所有 NSLog 行都用于调试。
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"deleteCT"]) {
        if (![self.detailItem.myCommodities count]) {
            NSLog(@"Testing");
            NSLog(@"myCommodities is empty:%@", [self.detailItem.myCommodities count]);
            NSLog(@"self.detailItem HAS a context:%@", [self.detailItem managedObjectContext]);
            NSLog(@"self.detailItem is of CommodityTypes:%@", [[self.detailItem entity] name]);
            NSLog(@"self.detailItem has a managed object model:%@", [[self.detailItem entity] managedObjectModel]);
            [[self.detailItem managedObjectContext] deleteObject:self.detailItem];
        }
    }
}

这是日志。我确实喜欢这种方法,
2012-04-18 10:15:49.544 FoodXJournal[13931:fb03] Testing

我符合删除条件,
2012-04-18 10:15:49.544 FoodXJournal[13931:fb03] myCommodities is empty:(null)

错误消息显示 '+entityForName: could not locate an NSManagedObjectModel for entity name 'CommodityTypes' , 但是 self.detailItem有上下文。
2012-04-18 10:15:49.545 FoodXJournal[13931:fb03] self.detailItem HAS a context:<NSManagedObjectContext: 0x6d7e740>

是的,self.detailItem是我正在考虑的实体类型:
2012-04-18 10:15:49.546 FoodXJournal[13931:fb03] self.detailItem is of CommodityTypes:CommodityTypes

而且,是的,该实体类型( NSLog(@"self.detailItem is of CommodityTypes:%@", [[self.detailItem entity] name]); )具有托管对象模型:

托管对象模型描述很长,所以我在这里只展示它的第一部分:
2012-04-18 10:15:49.565 FoodXJournal[13931:fb03] self.detailItem has a managed object model:(<NSManagedObjectModel: 0x6d73250>) isEditable 0, entities {
Accounts = "(<NSEntityDescription: 0x6d71120>) name Accounts, managedObjectClassName Accounts, renamingIdentifier Accounts, isAbstract 0, superentity name Grandparent, properties {\n    \"account_1\" = \"(<NSAttributeDescription: 0x6d6e9d0>), name account_1, isOptional 1, isTransient 0, entity Accounts,

向下滚动:
CommodityTypes = "(<NSEntityDescription: 0x6d71240>) name CommodityTypes, managedObjectClassName CommodityTypes, renamingIdentifier CommodityTypes, isAbstract 0, superentity name Grandparent, properties {\n    myCommodities = \"(<NSRelationshipDescription: 0x6d701f0>), name myCommodities, isOptional 1, isTransient 0, entity CommodityTypes, 
CommodityTypes在托管对象模型中定义。

那为什么[[self.detailItem managedObjectContext] deleteObject:self.detailItem];碰撞?!?

有什么原因我不能删除self.detailItem在prepareForSegue中?我是否需要为单元格或标签分配一个操作,然后以编程方式调用 segue?

最佳答案

Eureka !谢谢jrturton!它不是来自创建新托管对象的方法,但您的想法确实让我回去并在那条线之前和之后使用一堆断点。我把范围缩小到 NSEntityDescription *entity = [NSEntityDescription entityForName:@"CommodityTypes" inManagedObjectContext:self.managedObjectContext];在主 TableView 中。在启动时,应用程序委托(delegate)设置一个托管对象上下文并将其传递给主视图。我忘记了,当我从详细 View 切换到主视图的下一个实例时,我不一定要回到原始实例。我必须将托管对象上下文传递给下一个 View Controller 。

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"deleteCT"]) {
        if (![self.detailItem.myCommodities count]) {

            CommodityTypes * thisItem = self.detailItem;
            NSManagedObjectContext * thisContext = [thisItem managedObjectContext];
            FoodXCommodityTypesMasterViewController * nextView = [segue destinationViewController];

            [thisContext deleteObject:thisItem];

            nextView.managedObjectContext = thisContext;

        }
    }
}

这现在有效。

关于ios - 还有一个原因 : '+entityForName: could not locate an NSManagedObjectModel for entity name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10211839/

相关文章:

ios - Xcode 4.5 UITableView

ios - 如何将 Swift 中的 Core Data 与 Cloudkit 以及在许多设备上同步

ios - NSManagedObject 子类 - 无法识别的选择器发送到实例

ios - 在不同线程上访问 NSManagedObject 的 objectID?

ios - NSManagedObjects 什么时候被释放?

ios - 在不损害应用程序性能的情况下,从 Core Data 执行级联对象删除的有效方法是什么?

ios - 安装了 Google Drive API,但出现错误 "no such module ' GoogleAPIClient'"

ios - Facebook 分享 iOS Swift

ios - UIView 当它是 tableHeaderView 时更新,但当它是 subview 时不更新

ios - 核心数据、iCloud 和两个持久性存储失败