NSFetchedResultController在模态视图中修改核心数据

发布于 2025-01-24 13:02:41 字数 2166 浏览 1 评论 0原文

当我从模态视图中插入核心数据时,我正在遇到崩溃。

-[MyObject compare:]: unrecognized selector sent to instance

libc++abi: terminating with uncaught exception of type NSException
dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyObject compare:]: unrecognized selector sent to instance 0x282706ee0'
terminating with uncaught exception of type NSException
From all the reading I found this can be caused by the predicate or the sort descriptor and indeed in this case, it's the sort descriptor because if I remove it, the crash doesn't happen. But what I have is two different NSFetchedResultControllers in the view which I think is the root cause. Here's how its setup.

我们有三个核心数据实体

博客 邮政 Bloghaspost(与博客和帖子有关系)

“ Bloghaspost”的原因是因为它与许多关系之间的关系不仅有很多,而且还具有与某些位置和许可数据有关的额外数据。因此,它必须是它自己的桌子,而不仅仅是在博客和帖子上建立许多关系。

查看a(称其为blogview),并使用nsfetchedresultscontroller。 FetchRequest设置为Bloghaspost。我有一个将描述符设置为博客,因此我将获得与特定博客相关的所有Bloghaspost对象。然后,在CellForrowatIndExpath中,我能够根据该实体显示帖子数据。

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"blog" ascending:YES];
    [sortDescriptors addObject:sortDescriptor];
    [fetchRequest setSortDescriptors:sortDescriptors];

这起作用了,当显示Blogview时,显示了一个不错的表,列出了所有帖子。所以那里都很好。

然后,用户可以点击一个按钮来创建新帖子。这可以在博客视图模式下打开NewPost视图。因此,这两个视图仍然活跃。

创建了一个邮政对象,并且用户能够投入其数据。其中一个领域是将其关联到选择哪个“博客”应该是一部分的“博客”。对于每个关联,创建了一个博客对象。这是崩溃发生的时候。

由于PostView位于Blogview的顶部,因此Blogview仍然处于活动状态。因此,当我将一个新对象插入Blogview显示其合适的核心数据中时。

我认为这可能与每个fetchrequest的cachename有关,但是我确保它们确实是不同的,并且在任何情况下都无法解决问题。删除谓词也无济于事。同样,删除sortDeScriptor确实解决了问题。

我还尝试在插入后删除缓存,但这也无法解决。实际上,我还尝试过删除缓存,再次设置谓词,再次设置sortDescriptor,然后手动调用NSFetchedResultScontroller perfercther手动效果并发生错误。但是,这一点是我没有实例化新的fetedResultScontroller。我重复使用相同的一个。应该起作用。

好像我需要告诉第一个NSfetchedResultScontroller即将发生更改?

I'm encountering a crash when I insert a new entry into Core Data from a modal view.

-[MyObject compare:]: unrecognized selector sent to instance

libc++abi: terminating with uncaught exception of type NSException
dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyObject compare:]: unrecognized selector sent to instance 0x282706ee0'
terminating with uncaught exception of type NSException
From all the reading I found this can be caused by the predicate or the sort descriptor and indeed in this case, it's the sort descriptor because if I remove it, the crash doesn't happen. But what I have is two different NSFetchedResultControllers in the view which I think is the root cause. Here's how its setup.

We have three Core Data Entities

Blog
Post
BlogHasPost (has a relationship to Blog and Post)

The reason for "BlogHasPost" is because not only is it a many to many relationship but it also has extra data in it relating to some position and permission data. Therefore it had to be it's own table rather than just creating a many relationship on both Blog and Post.

View A (let's call it BlogView) and makes use of NSFetchedResultsController. The fetchRequest is set to BlogHasPost. I have a sort descriptor set to Blog so that I will get all the BlogHasPost objects related to a particular Blog. Then in the cellForRowAtIndexPath I'm able to show the Post data based on this entity.

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"blog" ascending:YES];
    [sortDescriptors addObject:sortDescriptor];
    [fetchRequest setSortDescriptors:sortDescriptors];

This works and when BlogView is shown it shows a nice table listing all the Posts. So all good there.

Then the user can tap a button to create a new Post. This opens up the NewPost view on top of the BlogView modally. Therefore both views are still active.

A Post object is created and the user is able to put in their data. One of the fields is to associate it to pick which "blogs" it should be part of. For each association a BlogHasPost object is created. This is when the crash happens.

Because PostView is on top of BlogView the BlogView is still active. Therefore when I insert a new object into core data that BlogView is showing it's having a fit.

I thought this might have been related to the cacheName for each fetchRequest but I ensured that these are indeed different and it did not resolve the issue in any case. Removing the predicate also did not help. Again, removing the sortDescriptor did fix the issue.

I've also tried deleting the cache after insert and that didn't solve it either. In fact I've also tried deleting the cache, setting the predicate again, setting the SortDescriptors again and then calling NSFetchedResultsController performFetch manually and the error occurs. However key to this is that I do NOT instantiate a new fetchedResultsController. I reuse the same one. Which should work.

It's as if I need to tell the first NSFetchedResultsController that changes are about to occur?

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

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

发布评论

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

评论(1

病毒体 2025-01-31 13:02:41

好吧,我想出了这个。这很奇怪。我必须将sortDeScriptor和谓词设置为MyObject中的另一个字段。例如,我可以使用ID字段(或UUID)或名称字段。您可以比较的东西。因此,对于谓词,我将其设置为blog.uuid,而不仅仅是帖子,而对于sortdeScriptor来说是相同的。

对我来说很奇怪的是,只是为andDeScriptor设置它无效。我必须更改描述符。但是我只是想要博客的帖子,所以我不确定为什么要使用uuid(我想我也可以使用objectid)。为什么不只是使用对象本身不起作用,我不确定。最初显示并具有其他对象时起作用。我认为这是某种独特的情况,适用于使用插入的sortdecriptors和prepticates(我认为更新的问题都有相同的问题)。

希望将来对其他人有所帮助。

Ok I figured this one out. It was an odd one. I had to set BOTH the sortDescriptors and the Predicate to a different field within MyObject. For instance I could use an id field (or uuid) or name field. Something that you can compare. So for the predicate I set it to blog.uuid instead of just post and the same for the sortdescriptor.

What's odd for me is that just setting it for the SortDescriptor did not work. I had to change the descriptor. Yet I just wanted the posts for the blog so I'm not sure why I had to use the uuid (I guess I could've used the objectID too). Why not just using the object itself didn't work I'm unsure. It works when initially displayed and has for other objects. I think this is some sort of unique case for when using sortdescriptors and predicates with an insert (I assume an updated would've had the same issue).

Hope this helps anyone else in the future.

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