开发者

Core Data syncronization procedure with Web service

开发者 https://www.devze.com 2023-04-10 08:57 出处:网络
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/re

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 lik开发者_Go百科e

    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 ??


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消