【问题标题】:NSObjectInaccessibleException after opening a uiviewcontroller打开 uiviewcontroller 后出现 NSObjectInaccessibleException
【发布时间】:2011-07-25 10:32:11
【问题描述】:

我正在开发一个应用程序,其中有一个自定义标签栏打开不同的表格视图。表格视图的信息由 JSON 远程提要提供。并使用自定义 nsmanagedobject 保存到 Core Data 中。

我在 didSelectRowAtIndexPath 中打开和关闭 uiview 后触发错误。详细视图在 tableview 上方打开,并且在角落有一个关闭按钮。该视图仅用于阅读文章,不能用于编辑、重写或删除。在我打开和关闭详细视图两次后出现错误。

日志说 NSManagedObject 已失效。但是如何,当我不删除或更新对象时。它发生在每个单元格行中。

我做错了什么?

日志:

* 由于未捕获的异常“NSObjectInaccessibleException”而终止应用程序,原因:“ID:0x75401c0 的 NSManagedObject 已失效。”
*
首次抛出时调用堆栈:
(
0 CoreFoundation 0x013d15a9 异常预处理 + 185
1 libobjc.A.dylib 0x01525313 objc_exception_throw + 44
2 核心数据 0x0117f120 -[NSFaultHandler _fireFirstAndSecondLevelFaultsForObject:withContext:] + 0
3 核心数据 0x01110933 _PF_FulfillDeferredFault + 499
4 核心数据 0x011149af _sharedIMPL_pvfk_core + 95
5 核心数据 0x011247c0 _pvfk_9 + 32
6 Ultima 0x00014bf7 -[ProgramTable tableView:cellForRowAtIndexPath:] + 4247
7 UIKit 0x004c1b98 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634
8 UIKit 0x004b74cc -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
9 UIKit 0x004cc8cc -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
10 UIKit 0x004c490c -[UITableView layoutSubviews] + 242
11 QuartzCore 0x0014fa5a -[CALayer layoutSublayers] + 181
12 QuartzCore 0x00151ddc CALayerLayoutIfNeeded + 220
13 石英核心 0x000f70b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
14 石英核心 0x000f8294 _ZN2CA11Transaction6commitEv + 292
15 QuartzCore 0x000f846d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
16 核心基础 0x013b289b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION
+ 27
17 核心基础 0x013476e7 __CFRunLoopDoObservers + 295
18 核心基础 0x013101d7 __CFRunLoopRun + 1575
19 核心基础 0x0130f840 CFRunLoopRunSpecific + 208
20 核心基础 0x0130f761 CFRunLoopRunInMode + 97
21 图形服务 0x01e6d1c4 GSEventRunModal + 217
22 图形服务 0x01e6d289 GSEventRun + 115
23 UIKit 0x0045ac93 UIApplicationMain + 1160
24 Ultima 0x000028e9 主要 + 121
25 Ultima 0x00002865 开始 + 53 )
终止调用抛出异常[切换到进程429线程0xec03]
sharedlibrary apply-load-rules all
当前语言:自动;目前客观-c

我的 cellForRowAtIndexPath 看起来像:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

ProgramCelle *cell = (ProgramCelle*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[ProgramCelle alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
int row = indexPath.row;
int section = indexPath.section;

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"HH:mm"];

if(section == 0){
    Program *p =[eight objectAtIndex:row];
    cell.primaryTitle.text = p.title;
    cell.primaryTitle.textColor = [UIColor blueColor];
    Scene *s = p.scene;
    cell.secondaryTitle.text = s.name;
    NSDate *dato = p.tidspunkt;

    NSString *time = [formatter stringFromDate:dato];
    cell.klokke.text = time;

}
if(section == 1){
    Program *p =[nine objectAtIndex:row];
    cell.primaryTitle.text = p.title;
    cell.primaryTitle.textColor = [UIColor blueColor];
    Scene *s = p.scene;
    cell.secondaryTitle.text = s.name;
    NSDate *dato = p.tidspunkt;

    NSString *time = [formatter stringFromDate:dato];
    cell.klokke.text = time;
}
if(section == 2){
    Program *p =[ten objectAtIndex:row];
    cell.primaryTitle.text = p.title;
    cell.primaryTitle.textColor = [UIColor blueColor];
    Scene *s = p.scene;
    cell.secondaryTitle.text = s.name;
    NSDate *dato = p.tidspunkt;

    NSString *time = [formatter stringFromDate:dato];
    cell.klokke.text = time;
}
if(section == 3){
    Program *p =[eleven objectAtIndex:row];
    cell.primaryTitle.text = p.title;
    cell.primaryTitle.textColor = [UIColor blueColor];
    Scene *s = p.scene;
    cell.secondaryTitle.text = s.name;
    NSDate *dato = p.tidspunkt;

    NSString *time = [formatter stringFromDate:dato];
    cell.klokke.text = time;
}
if(section == 4){
    Program *p =[twelve objectAtIndex:row];
    cell.primaryTitle.text = p.title;
    cell.primaryTitle.textColor = [UIColor blueColor];
    Scene *s = p.scene;
    cell.secondaryTitle.text = s.name;
    NSDate *dato = p.tidspunkt;

    NSString *time = [formatter stringFromDate:dato];
    cell.klokke.text = time;
}
if(section == 5){
    Program *p =[thirteen objectAtIndex:row];
    cell.primaryTitle.text = p.title;
    cell.primaryTitle.textColor = [UIColor blueColor];
    Scene *s = p.scene;
    cell.secondaryTitle.text = s.name;
    NSDate *dato = p.tidspunkt;

    NSString *time = [formatter stringFromDate:dato];
    cell.klokke.text = time;
}
if(section == 6){
    Program *p =[fourteen objectAtIndex:row];
    cell.primaryTitle.text = p.title;
    cell.primaryTitle.textColor = [UIColor blueColor];
    Scene *s = p.scene;
    cell.secondaryTitle.text = s.name;
    NSDate *dato = p.tidspunkt;

    NSString *time = [formatter stringFromDate:dato];
    cell.klokke.text = time;
}
if(section == 7){
    Program *p =[fifthteen objectAtIndex:row];
    cell.primaryTitle.text = p.title;
    cell.primaryTitle.textColor = [UIColor blueColor];
    Scene *s = p.scene;
    cell.secondaryTitle.text = s.name;
    NSDate *dato = p.tidspunkt;

    NSString *time = [formatter stringFromDate:dato];
    cell.klokke.text = time;
}
if(section == 8){

    Program *p =[sixteen objectAtIndex:row];
    cell.primaryTitle.text = p.title;
    cell.primaryTitle.textColor = [UIColor blueColor];
    Scene *s = p.scene;
    cell.secondaryTitle.text = s.name;
    NSDate *dato = p.tidspunkt;

    NSString *time = [formatter stringFromDate:dato];
    cell.klokke.text = time;
}
if(section == 9){
    Program *p =[seventeen objectAtIndex:row];
    cell.primaryTitle.text = p.title;
    cell.primaryTitle.textColor = [UIColor blueColor];
    Scene *s = p.scene;
    cell.secondaryTitle.text = s.name;
    NSDate *dato = p.tidspunkt;

    NSString *time = [formatter stringFromDate:dato];
    cell.klokke.text = time;
}
[formatter release];
return cell;
}

其中八、九、十 ... 是在 viewdidLoad 中发起并在 dealloc 中释放的 NSArray。

【问题讨论】:

  • 有什么理由不使用 NSFetchedResultsController 来管理 tableview 数据?这就是它的用途,尤其是对于带有部分的表格。
  • 没有理由。它更稳定吗?

标签: ios uitableview core-data nsmanagedobject


【解决方案1】:

您可能保留对象的时间不够长(也称为自动释放)。因此,当一段时间过去时,您会尝试访问不再存在的东西。尝试将您的数据传递给 NSMutableArray 并将其用作“tableView:cellForRowAtIndexPath”上的数据源。

【讨论】:

  • 我将它放在一个数组中,并且需要按日期对其进行排序,因为我有不同的部分,哪个部分代表一个日期。所有这些日期数组都在 dealloc 中释放,我是否应该将它们设置为自动释放而不是在 dealloc 中释放它们?
  • 让我们澄清一件事,只要您需要它们,您的阵列就应该存在。如果您要在“tableView:cellForRowAtIndexPath”中使用它,您怎么会谈论dealloc?
  • @lagos -- 您需要对作为控制器属性保存的数组使用自点表示法,例如self.eight 用于分配和参考。否则,您的数组可能会在没有警告的情况下释放。
【解决方案2】:

Core Data 中的NSObjectInaccessibleException 意味着您获得了对不在持久存储中的对象的引用。

通常,这意味着您已将其从上下文中删除,同时在其他地方保持对它的引用,例如对象属性或数组。

但是,如果您一开始没有将对象正确添加到商店,您也可能会收到错误消息。当您创建 NSManagedObject 实例而没有将其正确插入到有效上下文中时,就会发生这种情况。

无论如何,您的直接问题实际上是在您创建和/或保存这些托管对象的代码中,而不是在 tableview 控制器代码中。这只是发生错误的地方,因为这是您尝试访问无效托管对象的地方。

【讨论】:

    【解决方案3】:

    我自己发现的。当我关闭覆盖视图控制器时,我必须重新加载 tableview 的数据。这不是任何数组或删除任何对象的问题。 喜欢

    [myTable reloadData];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多