NSFetchedResultsController 和 NSManagedObjectIDResultType 崩溃

发布于 2024-09-17 07:17:21 字数 3190 浏览 4 评论 0原文

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

一切都按预期进行。问题是当涉及到从我的表视图(从上下文)添加/删除对象时。 这是我在 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 和 NSManagedObjectID 的问题。当我将其委托设置为 nil 时,它不会崩溃(甚至使用空委托方法也会崩溃)。

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

I´m currently working on an ipad app in which I fetch a lot of data with core data. To keep memory profile low I´m setting the requests resultType to NSManagedObjectIDResultType and grabbing objects with the -objectWithID: Method whenever I need real data.

Everything works as expected. The problem is when it comes to adding/removing Objects from my table view (from the context).
Thats a piece of code I call in my 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];
}

It throws me an exception: "* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[_NSObjectID_48_0 objectID]: unrecognized selector sent to instance 0x5e56900'"

Trace:

 #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

I assume this is an issue with the NSFetchedResultsController and NSManagedObjectIDs. When I set its delegate to nil its not crashing (even crashes with empty delegate methods).

Any idea? Would it be a good idea if I get rid of the NSFetchedResultsController and doing updates manually by observing NSManagedObjectContext notifications?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烈酒灼喉 2024-09-24 07:17:21

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

从苹果的文档来看,该方法:

objectAtIndexPath:

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

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

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

并替换

[self.managedObjectContext deleteObject: objectToDelete];

[self.managedObjectContext deleteObject: ID];

I think [fetchedResultsController objectAtIndexPath: indexPath]; is returning something that is not a NSManagedObjectID instance, but an Instance of NSObject.

From apple's documentation, the method:

objectAtIndexPath:

Returns the object at the given index
path in the fetch results.

not the ID of the object, try commenting out or deleting the line:

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

and replacing

[self.managedObjectContext deleteObject: objectToDelete];

with

[self.managedObjectContext deleteObject: ID];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文