Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syncing Database (sqlite) from WebService(Json/XML) for iOS

I have a Web Service and sqlite database. In this, web service will be used to store data inside database. Now I want to include sync functionality as - Whenever application starts at that time the database will start to load its table's data through web service.

Now after some time when I update my my web service the database will be updated accordingly. My question is that what are the best practices that I must follow for this update. Should I clear whole DB and start adding all rows again(I know this will take a lot time) but If not this then how do my database will add only particular data from the web service?

Thank you.

like image 379
Vish Avatar asked Jul 29 '13 05:07

Vish


3 Answers

What I suggest you is:

  1. store all your webservice content into db first when the app starts.
  2. display your content on the screen from db only.
  3. again when you need to refresh or recall your data just update the database.

Thus, you will always find all your fresh data into database.

like image 157
Niru Mukund Shah Avatar answered Nov 15 '22 19:11

Niru Mukund Shah


Downloading and updating the entire server data will prove expensive. It will use more bandwidth and prove costly to your customer. Rather than pushing the entire load (even for minor update), send a delta. I will suggest you to maintain version information.

  1. When application downloads the data from web service for a said version and store it successfully in the database, set the current updated version as well in the DB.
  2. When app starts the next time, make a light weight header request to get just the version info from the server. The server should respond to this header request with the latest data version number.
  3. Check the version from WS with the current application data version stored in the DB. If the server has an updated version, start the sync.
  4. The version change information should be delta i.e.
    • For new version, server should send only the information that is changed since the version available with the device.
    • You server should have capability to calculate the delta between two versions.
    • Delta information will typically have sections like, new data, updated data, deleted data etc.
    • Based on this, the iOS app will make the necessary CRUD(Create, Read, Update and Delete) operations on the DB data.
  5. Once the iOS app updates itself, then you can update the DB version to the latest received version from server. Until then let it remain dirty for proper error handling.

Hope that helps.

like image 33
Amar Avatar answered Nov 15 '22 19:11

Amar


I would recommend you use RestKit's superb Core Data support.

By using RKEntityMapping you can map your remote objects from JSON or XML directly to Core Data entities in your database.

RestKit will automatically maintain the database for you, inserting and updating entries as appropriate from your web service. (In my experience, I've found deleting objects requires a tiny bit of extra work depending on how RESTful your web service is).

RestKit definitely does have a learning curve attached, but it's well worth it: having deployed it a couple of times now, is definitely a much better solution than manually writing your own SQLite/Web Service syncing code.

like image 45
Jonathan Ellis Avatar answered Nov 15 '22 19:11

Jonathan Ellis