hardikdevios
hardikdevios

Reputation: 1789

Core data and server data sync

First of all in this question i am not making any complaints with any DB its just something i dont know and need to improve myself.

I have developed one app which contains coredata and it has almost 5 to 6 tables for eg jobs,employees etc etc .. ok now few points about coredata.

So now when in every 30 minutes i have to create one JSON which contains Array of table which should get latest DB records that user have performed during these 30 minutes like add update delete so i need one kind of mechanism where i can sync data with my server and server also identify that this row need to be added or updated or deleted.

I have tried few following possibility but still i am not getting success with perfection

  1. I have created 3 fields createdtimestamp,updatedtimestamp and deletedtimestamp so when user add one entry first time it will be createdtimestamp of device time becuse app can runn offline.
  2. So in respective actions i do update other timestamps if user update any record or delete any record.but in order to get updated with server we must have to get latest entries with the server timestamp but in this case if iser timestamp are not matched with server then this case failes.

So i need suggestions from this community. THANKS.

Upvotes: 1

Views: 525

Answers (1)

cjnevin
cjnevin

Reputation: 311

Your server would probably require a CRUD (create, read, update, delete) type API. Requests should be queued when offline and sent repeatedly until a valid response is received, server should handle duplicate requests gracefully.

To handle add, you could call a method like 'createJob' with some parameters and receive an id from the server which you could then use to populate the job_id field.

To handle changes made server-side, you essentially need a 'last update' timestamp which you store locally, you would sync with the server and provide the newest timestamp you have previously received (and stored) or zero (never received one). The server would then return everything that has changed after this point in time along with the new timestamp (last change) that the app will need to send next time. The response should include enough information in order to update the current state and resolve any conflicts.

To tackle the deleted state, your record could have a 'hidden' flag to indicate whether it has been deleted (this is known as a 'soft delete'), this helps with referential integrity, but if you are sure you can purge the record without breaking any relationships you could do this once you have received a valid 'deleteJob' response from the server.

Without architecting your solution there is not a great deal more help I can provide. However, there are various tools out there to help less experienced developers tackle this process, such as RESTKit. Server-side you could look at Node.js or ruby on rails or anything else that handles REST for you.

You can use different HTTP methods in order to achieve this without having multiple endpoints, as defined in RFC2616.

Upvotes: 4

Related Questions