【问题标题】:Yet another, reason: '+entityForName: could not locate an NSManagedObjectModel for entity name还有一个原因:'+entityForName: 找不到实体名称的 NSManagedObjectModel
【发布时间】:2012-04-30 00:13:07
【问题描述】:

我遇到以下错误:

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

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

我有一个包含静态表视图的详细视图控制器。表格视图的一个单元格标记为“删除”,并链接到名为“deleteCT”的segue。我希望应用程序删除self.detailItem 并在点击此单元格时转到主视图控制器。这是我的方法。所有 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]; 会崩溃?!?

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

【问题讨论】:

  • 我尝试用这三行替换问题行,以防出现访问器问题,但它仍然崩溃。 ` // [[self.detailItem managedObjectContext] deleteObject:self.detailItem]; NSManagedObjectContext * thisContext = [self.detailItem managedObjectContext]; NSManagedObject * thisItem = self.detailItem; [thisContext deleteObject:thisItem]; `
  • 你确定是那条线导致了崩溃吗?错误消息看起来像是来自创建托管对象的方法之一

标签: ios core-data nsmanagedobjectcontext detailsview master-detail


【解决方案1】:

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

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

        }
    }
}

现在可以了。

【讨论】:

  • 您不必创建指向 moc 的指针;您可以将 thisItem.managedObjectContext 用于删除消息和您在下一个控制器上设置的 var。事实上,如果你要传递任何类型的 mo,你也可以在下一个控制器中使用那个 mo 的 moc。至少有助于保持整洁。
猜你喜欢
  • 2011-05-14
  • 1970-01-01
  • 1970-01-01
  • 2012-07-20
  • 2011-10-29
  • 2012-06-20
  • 2010-12-31
  • 2011-03-21
相关资源
最近更新 更多