Restkit + Coredata - NSFetchedResultsControllerDelegate回调不单独为UPDATE操作触发 [英] Restkit + Coredata - NSFetchedResultsControllerDelegate callbacks not triggered for UPDATE operations alone

查看:139
本文介绍了Restkit + Coredata - NSFetchedResultsControllerDelegate回调不单独为UPDATE操作触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个奇怪的问题。我没有得到我的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]])){
/ **
注意:处理挂起的更改确保托管对象上下文生成可观察的
回调这对于维护在单个
对象映射操作中一致的任何类型的高速缓存很重要。由于MOC仅在处理聚合操作时保存,因此我们必须手动调用processPendingChanges
以防止使用相同的主键重新创建对象。
参见https://github.com/RestKit/RestKit/issues/661
* /
[self connectRelationships];
}
return success;
}

但我不能为自己的生活找出如何解决这个问题? Coz有目的吗?



有没有人面临同样的问题?如何解决?



感谢,
Raj Pawan

解决方案>

我有一个错误从我的部分和1其他的东西(Ignorance),我不知道,因为我面临这个问题:



错误:


尽管论坛上有很多讨论,但我错误地列出了我第二个列出的项目:





  • 只有单个受管对象上下文中正在进行这些

    修改。


    < blockquote>



我错误地记录并发现我在同一上下文中!



无知:


我做了一些挖掘RK代码的思考有什么鱼腥味在那里,检查布莱克Watters的意见和他的提交4b394f8c1e1f,看看是否现在删除的早期代码(调用-processPendingChanges)是否导致任何问题,不让代理被告知更新。 p>

发现这确实是在一个单独的线程,是的它有自己的MOC,我错过了。接下来要做的是简单的,我实现了合并更改的机制MOC从不同的线程进入主MOC。但是这也没有工作!



原因证明,我处于应用程序开发的非常初始阶段。我只是映射json响应与coredata对象使用RestKit和我无处可用!我只是在GDB中记录coredata对象,他们始终处于故障状态!我依靠 -objectLoader 回调和 NSNotification对象看到确实有一个 Update 可用。在测试的某个时刻,我碰巧记录了被管理对象的一个​​属性,它在通知回调中被改变,然后被合并回主MOC。这会使受管对象发生故障,并加载托管对象的所有属性。现在当副线程MOC更改与主线程MOC合并时,我的NSFetchedResultsControllerDelegate回调开始触发。


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

解决方案

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.

这篇关于Restkit + Coredata - NSFetchedResultsControllerDelegate回调不单独为UPDATE操作触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆