【问题标题】:NSInternalInconsistencyException when saving second entity in iOS app在 iOS 应用程序中保存第二个实体时出现 NSInternalInconsistencyException
【发布时间】:2011-11-29 03:13:33
【问题描述】:

我在这里经历了很多问题,但仍然不清楚。

我的应用程序(标签栏应用程序)中有 5 个选项卡,每个选项卡负责将一些细节推送到 1 个表中。所以实际上每个选项卡都有一个表。

我正确初始化了managedObjectContext,并成功地将数据保存在实体中。现在,当我尝试将数据推送到第二个表中时,我得到了这个异常:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“+entityForName:找不到实体名称“OtherDetail”的 NSManagedObjectModel*

是否需要一次性将数据推送到所有实体中?

(void)persistOtherDetail:segCtrlValue:genInfoId:type:labelValue{
NSLog(@"Persisting %@",type);

NSLog(@"seg control value %@", segCtrlValue);

OtherDetail *otherDetail = (OtherDetail *)[NSEntityDescription insertNewObjectForEntityForName:@"OtherDetail" inManagedObjectContext:managedObjectContext];
otherDetail.enteredValue = segCtrlValue;

otherDetail.genInfoId = genInfoId;
otherDetail.checklistDesc = labelValue;
otherDetail.checklistName = type;

NSError *error;

if (![managedObjectContext save:&error])
{
    NSLog(@"Problem saving: %@", [error localizedDescription]);
}
}

【问题讨论】:

  • 您能否将尝试将数据持久化到“OtherDetail”的代码部分发布?
  • 你的方法签名伤了我的眼睛
  • @VinceBurn 你在谈论争论吗?我需要一个可以传递这 4 个参数的方法,因为我需要从不同的选项卡一次又一次地调用它。
  • 是的,这不是参数的数量,而是它们都被挤压并且看起来好像缺少某些部分的事实- (void)persistOtherDetail:(int)segCtrlValue genInfoId:(int)anId withChecklistType:(int)aType forLabelValue:(NSString *)theLabelTxt 也许我是那些纯粹主义者并且弄错了那个,但是我喜欢它在 Apple 框架中的完成方式,每种方法都在讲述一个故事。

标签: ios objective-c iphone cocoa-touch core-data


【解决方案1】:

在您的代码中没有任何内容立即引起我的注意,但根据我的经验,该错误经常发生,因为 managedObjectContext 实例无效,或者因为引用实体或实体属性时的简单拼写错误。对于第一个原因,您可以设置对上下文的检查(在创建 OtherDetail 对象之前),如果为 nil 则实例化它:

if (managedObjectContext == nil) { managedObjectContext = [(MyAppDelegateName *)[[UIApplication sharedApplication] delegate] managedObjectContext];

否则,我会回去检查任何拼写错误。不过,最有可能的是,您在第一个选项卡视图控制器中正确设置了 managedObjectContext,但在另一个选项卡视图控制器中没有正确设置。

【讨论】:

  • 瞧,它最终解决了我的问题。谢谢你:),我在其他问题中也发现了这个建议的修复,只是不知道在哪里放置这段代码。我知道这听起来像是一个愚蠢的新手。无论如何谢谢!
猜你喜欢
  • 1970-01-01
  • 2014-02-19
  • 1970-01-01
  • 2021-12-13
  • 2023-03-03
  • 2020-11-18
  • 1970-01-01
  • 2019-02-18
  • 2022-11-23
相关资源
最近更新 更多