-1

is it possible to use RESTKit for two way synchronization? I played aroud with RESTKit and CoreDate. Now I can download all data from my REST service and all changes (create/modify/delete objects) in CoreDate will be overwritten by RESTKit.

Now I want to choose between both versions (the local version or the remote version). How do I do this? Is there a way to manipulat the mapping, or something like that?

update: My app should synchronize after some changes or after a specific delay (not difficult). Every object has a change date. Until now I want to keep the newer one (if they are equal the local one). I hope RestKit is made for changing the strategy how it merges objects. something like a block I can set, where I get both objects and can return the merged object.

What I got so far: I load the object via RestKit but do not persist them. Also I setup a CoreData store where I store the local objects. After loading the remote object I start to synchronize my self. First searching for pairs and then decide which to take, delete, create, overwrite, and so on...

But this is a big bunch of work and I think RestKit is doing something similar. Why not simply changing the strategy of RestKit for my requirements.

Well this would be the "syncing down" thing. After this I could write the synchronized data set back to the service. The changes are not very frequently so I will not have to check for new changes.

I hope now it's a little bit clearer

Obenland
  • 856
  • 16
  • 28
  • Not clear what you want. When a change is made locally do you want it to be pushed to the server? If both local and server have changed, how do you decide who wins? – Wain Sep 23 '13 at 12:55
  • until now the local objects will be overwritten by the remote objects regardless of local changes. I want to merge both data sets. But I don't know how... – Obenland Sep 23 '13 at 13:58
  • Bit what are you trying to achieve? – Wain Sep 23 '13 at 17:18
  • I have updated the description of my question. I hope now it's a little bit clearer – Obenland Oct 07 '13 at 09:23

2 Answers2

1

What you really want to do is validate the data coming in.

Since RestKit is using CoreData it automatically uses the validation built into CoreData (see here)

Here is an example that will ensure that the date never gets changed to an earlier one.

- (BOOL) validateChangeDate:(id *)ioValue error: (NSError **)outError {

    if ([[*ioValue laterDate:self.changeDate] compare:self.changeDate] == NSOrderedSame)
        *ioValue = self.changeDate;
    return YES;
}

Note: There may be better/faster ways to test to see if we need to change the date
Note 2: According to the docs, if RestKit sees a value rejected, it fails the entire object, which we don't want, so that's why we always return YES and just change the incoming date
Note 3: We also don't want to change the value unless we have to because of what the CoreData docs say here

StaRbUck42
  • 117
  • 1
  • 8
0

You may be able to leverage KVC validation to achieve this as it allows you to check and then edit or deny the change for each object and key. Check these docs.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Well, that's cool. But the context of the remote object is missing. – Obenland Oct 07 '13 at 11:09
  • What context information? Basically you would need to merge the object and reject if the remote date was later than the local date? – Wain Oct 07 '13 at 11:18
  • Exactly. But I don't get the change date of the remote object. There is only the new value as parameter of the validation methods. The change date is a property of the object – Obenland Oct 07 '13 at 11:25
  • ? So you get the change data in your incoming data? And it gets passed to the validation method? Where you can check it against the current data? I'm not clear on what you have locally and what you receive from remote... – Wain Oct 07 '13 at 12:57