1

I'm developing an application that needs to be syncronized with remote database. The database is connected to the a web-based application that user able to modify some records on the web page.(add/remove/modify) User also able to modify the same records in mobile application. So each side (server - client) must be keep the SAME latest records when an user press the sync button in mobile app. Communication between server and client is provided by Web Serives.(SOAP) and i am not able to change it because of it is strict requirements. (i know this is the worst way that can be used). And another requirement is the clients are not able to delete the server records.

I already be familiar with communicating web service (NSURLConnection), receiving data (NSData) and parsing it. But i could not figure out how the syncronization procedure should be. I have already read this answer which about how i can modify server and client sides with some extra attributes (last_updated_date and is_sync)


Then i could imagine to solve the issue like:

As a first step, client keeps try to modify the server records with sending unsyncronized ones. New recoords are directly added into DB but modified records shoud be compared depending on last_updated_date. At the end of this step, server has the latest data.

But the problem is how can manage to modify the records in mobile app. I thought it in a two way:

  1. is the dummiest way that create a new MOC, download all records into this and change with existing one.

  2. is the getting all modified records which are not in client side, import them into a new MOC and combine these two. But in this point i have some concerns like

    There could be two items which are replicated (old version - updated version)

    Deleted items could be still located in the main MOCs.

    I have to connect multiple relationships among the MOCs. (the new record could have more than 4 relationships with old records)

So i guess you guys can help me to have another ideas which is the best ??

Community
  • 1
  • 1
CanP
  • 65
  • 6

1 Answers1

1

Syncing data is a non-trivial task.

There are several levels of synchronization. Based on your question I am guessing you just need to push changes back to a server. In that case I would suggest catching it during the -save: of the NSManagedObjectContext. Just before the -save: you can query the NSManagedObjectContext and ask it for what objects have been created, updated and deleted. From there you can build a query to post back to your web service.

Dealing with merges, however, is far more complicated and I suggest you deal with them on the server.

As for your relationship question; I suggest you open a second question for that so that there is no confusion.

Update

Once the server has finished the merge it pushes the new "truth" to the client. The client should take these updated records and merge them into its own changes. This merge is fairly simple:

  1. Look for an existing record using a uniqueID.
  2. If the record exists then update it.
  3. If the record does not exist then create it.

Ignoring performance for the moment, this is fairly straight forward:

  1. Set up a loop over the new data coming in.
  2. Set up a NSPredicate to identify the record to be updated/created.
  3. Run your fetch request.
  4. If the record exists update it.
  5. If it doesn't then create it.

Once you get this working with a full round trip then you can start looking at performance, etc. Step one is to get it to work :)

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
  • Thanks for responding me @Marcus and i did some modification to make it more clear. I'm looking forward to your ideas... – CanP Oct 04 '11 at 00:43
  • Thanks again @Marcus, so when we merge modification in client side, i guess i need to use a new MOC. Some (old) records could be located (related) within new records which i need to pass these records from one MOC to another one. And i guess it causes a problem, how could i solve it? – CanP Oct 07 '11 at 10:21
  • Start off with a single MOC. You only use multiple MOCs when you are dealing with multiple threads (there are exceptions of course). I would look at getting it to work first then deal with performance and it is during the performance tuning that you would start to grok multiple MOCs. – Marcus S. Zarra Oct 07 '11 at 16:34
  • Thanks you @Marcus, I have almost done with it. But there is only one problem, i haven't figure out. It is possible to delete a record in server side and when the record is deleted, it's gone permanantly (not modify deleted flag). So how the client could know that the record in its DB was deleted or not? – CanP Oct 28 '11 at 10:19
  • @CanP depends, if you get a full feed every time then do a reverse flag, any record not touched/updated gets deleted. If the server does not send you a full feed every time then that would be an issue that is not solvable without some kind of direction from the server. – Marcus S. Zarra Oct 28 '11 at 22:46