Viren Rajput
Viren Rajput

Reputation: 5736

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. 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. 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. (this block gets executed after the API operation is successful and just before restkit performs the mapping)

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.

This is 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. 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. But for EntityC and EntityD, there is no direct relation to RootEntity which makes writing a fetch request difficult.

Also, I have setup cascaded deletion in relationships. So, when rootEntity gets deleted, all children get deleted. I think this deletion would be less expensive than making those fetch requests.

Upvotes: 1

Views: 661

Answers (1)

Wain
Wain

Reputation: 119031

You should instead us a fetch request block which is executed by RestKit on success (not failure, be careful with your response descriptors as they dictate what success is) and deletes everything which matches the fetch but isn't new in the just received response.

Upvotes: 1

Related Questions