Search code examples
core-datarestkitsynchronization

Restkit.Delete on server sided changes


I use RestKit to receive data from my web server and storing it in Core Data.

Everything works fine, also the object mapping.

But now I have a question/problem. I want to sync my device with the data from the web server. The way with new or updated objects is clear, because RestKit controls it for me. But how to delete objects in Core Data? There is a way using the DELETE method. But what is, if some objects were deleted on the server?

I want to make a GET request with the timestamp of the last sync to receive all data changes (delta).

  • new objects (OK)
  • modified objects (OK)
  • deleted objects (How?)

How to delete objects in Core Data with JSON payload from the server. Is there a possibility to prevent the save when a delete attribute of an object in JSON is set? Is there a way to delete this object from Core Data.


Solution

  • Solution n°1: The removed attribute

    Imho it wouldn't be a good solution to remove every local objects that doesn't appear in the request reponse. So I just add an attribute removed (not deleted because it's reserved by Core Data) and only display Object[removed=FALSE].

    In a fetch request, it would be:

    removed == FALSE
    

    Solution n°2: what-should-I-delete?

    You could make a simple page in your API that would indicate what to delete on your application. For instance:

    [
      {'type': 'post', 'id': 5},
      {'type': 'comment', 'id': 10},
    ]
    

    Then, you would fetch objects by postID or commentID and delete them from the store. You'll have to find a way to confirm the deletion to the server, as the list will grow.