简体   繁体   English

在Restkit执行映射操作之前,删除Core数据中的现有对象

[英]Delete existing objects in Core data before Restkit performs mapping operation

Basically I am trying to deal with orphan objects in this case. 基本上,我试图在这种情况下处理孤立对象。

I have some existing objects in my core data store. 我的核心数据存储区中有一些现有对象。 And before performing the API request operation I delete all the objects for that particular entity. 在执行API请求操作之前,我删除了该特定实体的所有对象。 So when I fetch new objects from the server, my local data is in sync with the new data object received from the server. 因此,当我从服务器获取新对象时,我的本地数据与从服务器接收到的新数据对象同步。

But there can be a case, when my API request operation fail. 但是有可能我的API请求操作失败。 In such a case, I cannot afford to lose the existing objects in my local database. 在这种情况下,我不能失去本地数据库中的现有对象。 Hence I considered using undo operation for that. 因此,我考虑为此使用撤消操作。

Now I tried using setWillMapDeserializedResponseBlock for performing this. 现在,我尝试使用setWillMapDeserializedResponseBlock执行此操作。 (this block gets executed after the API operation is successful and just before restkit performs the mapping) (在API操作成功之后并且在restkit执行映射之前,将执行此块)

So in this block, I perform deletion of the existing objects in the entity. 因此,在此块中,我将删除实体中的现有对象。

But after the mapping is completed, the objects that were missing in the server response, but were present in the local store still exist. 但是,在映射完成之后,服务器响应中缺少但本地存储中存在的对象仍然存在。 (hence the deletion of existing objects from the local store didn't affect the store). (因此,从本地存储中删除现有对象不会影响该存储)。 After explicitly calling the saveToPersistantStore method in the setWillMapDeserializedResponseBlock , the changes do not get reflected. 显式调用后saveToPersistantStore的方法setWillMapDeserializedResponseBlock ,所做的更改不会得到反映。

This is setWillMapDeserializedResponseBlock : 这是setWillMapDeserializedResponseBlock

    {
    // start undo grouping
    [[[NSManagedObjectContext MR_defaultContext] undoManager] beginUndoGrouping];
    [[[NSManagedObjectContext MR_defaultContext] undoManager] setActionName:@"undo deleting objects"];

    // perform deletion of objects
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"..."];
    [Entity MR_deleteAllMatchingPredicate:predicate];

    // end undo grouping
    [[[NSManagedObjectContext MR_defaultContext] undoManager] endUndoGrouping];
    }

How can I approach this problem in a different way? 我该如何以不同的方式解决这个问题?

Updated: 更新:

Using fetch Request Blocks is not feasible since there are a lot of entities in a tree like hierarchy. 使用提取请求块是不可行的,因为树状层次结构中有许多实体。

My structure for core data is like follows: 我的核心数据结构如下:

RootEntity
- identifier

EntityB
- root_identifier

EntityC
- b_identifier

EntityD
- c_identifier

The parameter for the api call is the identifier for RootEntity. api调用的参数是RootEntity的标识符。 I get all the entities in the response. 我得到了响应中的所有实体。 FetchRequests can be written easily for RootEntity and EntityB as they have an attribute for storing RootEntity 's identifier. FetchRequests可以轻松地为RootEntityEntityB编写,因为它们具有用于存储RootEntity标识符的属性。 But for EntityC and EntityD , there is no direct relation to RootEntity which makes writing a fetch request difficult. 但对于EntityCEntityD ,没有直接的关系RootEntity这使得编写为获取请求困难。

Also, I have setup cascaded deletion in relationships. 另外,我在关系中设置了级联删除。 So, when rootEntity gets deleted, all children get deleted. 因此,当rootEntity被删除时,所有子项都将被删除。 I think this deletion would be less expensive than making those fetch requests. 我认为这种删除将比发出那些获取请求便宜。

相反,您应该给我们提供一个获取请求块 ,该请求由RestKit成功执行(不是失败,请小心您的响应描述符,因为它们指示成功是什么),并删除与该获取匹配但在刚收到的响应中不是新内容的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM