【问题标题】:iphone core data deletion causes a crashiphone核心数据删除导致崩溃
【发布时间】:2010-09-14 16:42:36
【问题描述】:

我有一个包含 2 个字段的核心数据对象“实体”——“名称”(一个 NSString *)和“类型”(一个 EntityType)。 EntityType 是另一个具有 1 个字段的核心数据对象 - 名称 (NSString *)。
我正在使用与以下代码功能相似的代码创建和删除实体对象 -

Entity *e1 = [NSEntityDescription insertNewObjectForEntityName:"@Entity" inManagedObjectContext: context]

EntityType *type = [NSEntityDescription insertNewObjectForEntityName:"@EntityType" inManagedObjectContext: context]

e1.type = type;

现在我删除实体对象而不分配任何值或保存到持久存储。

if([e1.name length] == 0) {

  [context deleteObject:e1];

  NSError *nil;
  if(![context:save &error] {
    // log error
  }
}

此时,当我尝试删除和保存对象时,我看到了崩溃。 (由于未捕获的异常 'NSRangeException' 原因而终止:[NSCFArray removeObjectAtIndex]: index(0) beyond bounds (0)]

非常感谢任何指针/建议。

这是请求的堆栈跟踪 -

#0  0x3266bdf4 in objc_exception_throw ()
#1  0x32d73b32 in +[NSException raise:format:arguments:] ()
#2  0x32d73ad2 in +[NSException raise:format:] ()
#3  0x33f4a710 in _NSArrayRaiseBoundException ()
#4  0x33f7ba3c in -[NSCFArray removeObjectAtIndex:] ()
#5  0x30537dc6 in -[NSFetchedResultsController(PrivateMethods) _removeObjectInFetchedObjectsAtIndex:] ()
#6  0x3053b50a in -[NSFetchedResultsController(PrivateMethods) _postprocessDeletedObjects:] ()
#7  0x3053aa9a in -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] ()
#8  0x33f765d8 in _nsnote_callback ()
#9  0x32d9e510 in _CFXNotificationPostNotification ()
#10 0x33f741b2 in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
#11 0x304b4388 in -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] ()
#12 0x3050768a in -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:] ()
#13 0x3049c2b0 in -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] ()
#14 0x304cb186 in -[NSManagedObjectContext save:] ()
#15 0x00006d5a in -[MyAppViewController goToMain] (self=0x11b730, _cmd=0x174b3) at /Users/Me/Projects/MyApp/Classes/MyAppViewController.m:611



2010-09-15 17:37:10.053 MyApp[1273:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray removeObjectAtIndex:]: index (0) beyond bounds (0)'
2010-09-15 17:37:10.066 MApp[1273:207] Stack: (
    853417245,
    845594132,
    852966195,
    852966099,
    ............
     .............

【问题讨论】:

  • 请添加崩溃的堆栈跟踪

标签: iphone core-data


【解决方案1】:

错误来自 FRC 的 fetchedResults 数组。我怀疑这是由于您没有正确实现 FRC 委托方法造成的,因此当 tableview 尝试更新时,它会尝试访问空数组的零索引。

当您将 FRC 附加到任何上下文时,FRC 将在您对上下文进行任何更改(例如删除对象)时自动更新(这就是跟踪堆栈中的通知正在执行的操作,告诉 FRC 进行更新。 )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 2020-03-26
    • 1970-01-01
    相关资源
    最近更新 更多