Restkit + Coredata

编程入门 行业动态 更新时间:2024-10-16 00:25:05
Restkit + Coredata - 仅为UPDATE操作未触发NSFetchedResultsControllerDelegate回调(Restkit + Coredata - NSFetchedResultsControllerDelegate callbacks not triggered for UPDATE operations alone)

我这里有一个奇怪的问题。 我没有得到我的NSManagedObject上的“更新”操作的回调,但是在该实体类型的集合中插入或移除的任何对象将触发委托回调。

在我继续进一步讨论之前,我想告知我的设置:

NSFetchedResultsController已正确配置。 确保外部修改的属性不是此Apple文档所要求的fetchedResultsController的任何排序键:

当对象的状态更改时,将报告更新,但更改的属性不是排序键的一部分。

只有一个托管对象上下文发生了这些修改。

由于正在向委托报告插入和删除操作,因此我认为更新操作有些可疑

我在RKLogs的帮助下深入挖掘了Restkit代码,以查看映射的确切位置以及更新coredata对象的位置,以找出未获得更新委托回调的原因。

在类RKManagedObjectMappingOperation -performMapping方法中,Blake Watters先生已经记录了更新时未触发MOC回调的原因:

- (BOOL)performMapping:(NSError **)error { BOOL success = [super performMapping:error]; if ([self.objectMapping isKindOfClass:[RKManagedObjectMapping class]]) { /** NOTE: Processing the pending changes here ensures that the managed object context generates observable callbacks that are important for maintaining any sort of cache that is consistent within a single object mapping operation. As the MOC is only saved when the aggregate operation is processed, we must manually invoke processPendingChanges to prevent recreating objects with the same primary key. See https://github.com/RestKit/RestKit/issues/661 */ [self connectRelationships]; } return success; }

但我不能为自己的生活弄清楚如何解决这个问题? 因为它有目的地完成了吗?

有没有人遇到同样的问题? 我如何解决它?

谢谢,Raj Pawan

I have a strange problem here. I am not getting the callbacks for "Update" operations on my NSManagedObject, but where as any objects inserted into or removed from the collection of that entity type would trigger the delegate callbacks.

Before I proceed with the question further, I would like to inform about my setup:

NSFetchedResultsController is properly configured. Made sure that the property which is being modified externally is not any of the sort keys for the fetchedResultsController as required by this Apple documentation:

An update is reported when an object’s state changes, but the changed attributes aren’t part of the sort keys.

There is only single managed object context in which these modifications are happening.

Since insert and delete operations are being reported to the delegate, I presume there is something fishy about the Update operations

I was drilling down the Restkit code with help of RKLogs to see where exactly the mapping happens and where the coredata object is being updated to find out the reason why am not getting the update delegate callbacks.

In the class RKManagedObjectMappingOperation -performMapping method, Mr. Blake Watters has documented the reason why MOC callbacks are not triggered upon updates:

- (BOOL)performMapping:(NSError **)error { BOOL success = [super performMapping:error]; if ([self.objectMapping isKindOfClass:[RKManagedObjectMapping class]]) { /** NOTE: Processing the pending changes here ensures that the managed object context generates observable callbacks that are important for maintaining any sort of cache that is consistent within a single object mapping operation. As the MOC is only saved when the aggregate operation is processed, we must manually invoke processPendingChanges to prevent recreating objects with the same primary key. See https://github.com/RestKit/RestKit/issues/661 */ [self connectRelationships]; } return success; }

But I cannot for the life of myself figure out how to fix this? Coz it was done purposefully?

Has anyone faced same problem? How do I fix it?

Thanks, Raj Pawan

最满意答案

我有一个错误和另外一件事(无知),我不知道,因为我遇到了这个问题:

错误: 尽管在论坛上进行了很多讨论,但我说错了第二个列出的项目:

这些只有一个托管对象上下文 正在进行修改。

我错误地记录并发现我在同一个环境中! 我这么傻!

无知: 我做了一些挖掘RK代码的想法,认为那里有些可疑,检查了Blake Watters的评论和他的提交4b394f8c1e1f以查看现在删除的早期代码(调用-processPendingChanges)是否导致任何问题并且不让代表被告知有关更新。

发现这确实是在一个单独的线程上,是的,它有自己的MOC,我错过了。 接下来要做的很简单,我实现了将MOC的变化从不同的线程合并到主MOC的机制 。 但这也不起作用!

原因是我处于应用程序开发的最初阶段。 我只是使用RestKit映射json响应和coredata对象,我无处利用它! 我只是在GDB中记录coredata对象,它们始终处于故障状态 ! 我依靠-objectLoader回调和NSNotification object来查看确实有可用的更新 。 在测试的某个时刻,我碰巧记录了托管对象的一个​​属性,该属性在Notification回调中被更改,然后再合并回主MOC。 这会导致托管对象出错并加载托管对象的所有属性。 现在,当辅助线程MOC更改与主线程MOC合并时,我的NSFetchedResultsControllerDelegate回调开始触发。

There was 1 mistake from my part and 1 other thing (Ignorance) which I was not aware of and due to which I faced this problem:

Mistake: Despite many discussion all over the forums, I was wrong to state my second listed item:

There is only single managed object context in which these modifications are happening.

I had wrongly logged and found out that I was in the same context! So dumb of me!

Ignorance: I did some digging through the RK code thinking something fishy is going on there, checked Blake Watters' comments and his commit 4b394f8c1e1f to see if the earlier code there (call to -processPendingChanges) which is now removed was causing any issue and not letting the delegate to be informed about updates.

Found out that this was indeed on a separate thread and yes it had its own MOC which I had missed out. Next thing to do was simple, I implemented the mechanism to merge changes of a MOC from different thread into main MOC. But this did not work either!

The reason turns out that I am in a very initial stages of the development of application. I am just mapping the json response with coredata objects using RestKit and I am nowhere utilising it! I was just logging the coredata objects in GDB and they remained in their fault state always! I was relying upon the -objectLoader callbacks and the NSNotification object to see that there was indeed an Update available. At some point while testing I happened to log a property of the managed object which is changed in the Notification callback before it was merged back to the main MOC. This faulted the managed object and loaded all properties of the managed object. Now when the secondary thread MOC changes were merged with the main thread MOC, my NSFetchedResultsControllerDelegate callbacks started triggering.

更多推荐

本文发布于:2023-08-05 23:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1440834.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Restkit   Coredata

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!