NSFetchedResultsController和NSManagedObjectIDResultType崩溃

时间:2010-08-29 18:23:41

标签: objective-c core-data nsfetchedresultscontroller nsmanagedobject

我目前正在开发一个ipad应用程序,我在其中使用核心数据获取大量数据。为了保持内存配置文件的低,我将请求resultType设置为NSManagedObjectIDResultType,并在需要实际数据时使用-objectWithID:Method获取对象。

一切都按预期工作。问题是从我的表视图(从上下文)添加/删除对象。 这是我在tableView中调用的一段代码:commitEditingStyle:forRowAtIndexPath:

if(editingStyle == UITableViewCellEditingStyleDelete) 
{        

     NSManagedObjectID *ID = [fetchedResultsController objectAtIndexPath: indexPath];
  NSManagedObject *objectToDelete = [self.managedObjectContext objectWithID: ID];

        [self.managedObjectContext deleteObject: objectToDelete];

  [self.managedObjectContext save: nil];
}

它抛出了一个异常:“ *因未捕获的异常而终止应用程序'NSInvalidArgumentException',原因:'* - [_ NSObjectID_48_0 objectID]:无法识别的选择器发送到实例0x5e56900'”

跟踪:

 #0 0x9953cef6 in __kill
#1 0x9953cee8 in kill$UNIX2003
#2 0x995cf62d in raise
#3 0x995e56e4 in abort
#4 0x95826fda in __gnu_cxx::__verbose_terminate_handler
#5 0x02d9d61c in _objc_terminate
#6 0x9582517a in __cxxabiv1::__terminate
#7 0x958251ba in std::terminate
#8 0x958252b8 in __cxa_throw
#9 0x02d9d3d8 in objc_exception_throw
#10 0x02c84a5b in -[NSObject doesNotRecognizeSelector:]
#11 0x02c01676 in ___forwarding___
#12 0x02c009f2 in __forwarding_prep_0___
#13 0x0281f0f7 in -[_PFBatchFaultingArray arrayFromObjectIDs]
#14 0x0281fe4b in -[_PFMutableProxyArray managedObjectIDAtIndex:]
#15 0x0281ec8e in -[_PFMutableProxyArray indexOfManagedObjectForObjectID:]
#16 0x028823b8 in -[NSFetchedResultsController(PrivateMethods) _preprocessDeletedObjects:deletesInfo:sectionsWithDeletes:]
#17 0x02885159 in -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:]
#18 0x00065586 in _nsnote_callback
#19 0x02beb165 in _CFXNotificationPostNotification
#20 0x0005c2ca in -[NSNotificationCenter postNotificationName:object:userInfo:]
#21 0x027d838d in -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:]
#22 0x0283fe83 in -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:]
#23 0x027b9af6 in -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:]
#24 0x027f4a61 in -[NSManagedObjectContext save:]
#25 0x0035d361 in -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:]
#26 0x00306cee in -[UIApplication sendAction:to:from:forEvent:]
#27 0x0038043e in -[UIControl sendAction:to:forEvent:]
#28 0x003828c0 in -[UIControl(Internal) _sendActionsForEvents:withEvent:]
#29 0x0038146d in -[UIControl touchesEnded:withEvent:]
#30 0x00325de8 in -[UIWindow _sendTouchesForEvent:]
#31 0x0030b643 in -[UIApplication sendEvent:]
#32 0x003131d8 in _UIApplicationHandleEvent
#33 0x0351617c in PurpleEventCallback
#34 0x02bd289c in CFRunLoopRunSpecific
#35 0x02bd18a8 in CFRunLoopRunInMode
#36 0x0351489d in GSEventRunModal
#37 0x03514962 in GSEventRun
#38 0x00311372 in UIApplicationMain
#39 0x00002436 in main at main.m:14

我认为这是NSFetchedResultsController和NSManagedObjectIDs的问题。当我将其委托设置为nil时它没有崩溃(甚至使用空委托方法崩溃)。

有什么想法吗?如果我摆脱NSFetchedResultsController并通过观察NSManagedObjectContext通知手动进行更新,这是一个好主意吗?

1 个答案:

答案 0 :(得分:0)

我认为[fetchedResultsController objectAtIndexPath:indexPath];返回的东西不是NSManagedObjectID实例,而是NSObject的实例。

从Apple的文档中,方法:

  

objectAtIndexPath:

     

返回给定索引处的对象   获取结果中的路径。

不是对象的ID,请尝试注释掉或删除该行:

NSManagedObject *objectToDelete = [self.managedObjectContext objectWithID: ID];

并替换

[self.managedObjectContext deleteObject: objectToDelete];

[self.managedObjectContext deleteObject: ID];