1

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.

  • Coredata automatically manages primary key management we dont need to create the PK in every table
  • Myserver contains primary key for e.g. job_id , emp_id which is auto increment Now in my app i have to sync my data with server, well when i add any entry in my table the respective fields job or emp ids are going to be zero as expected i cant create my own value in this fields.

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks mate but how should i know which data is going to be updated becuase the id will zero in client end and when server return two records how i can assure that it will need to be added or need to be updated ?
If the id is zero, it would be an add, if non-zero it would be an update/delete. You should make sure any pending adds are sent prior to updating your data locally, just in case you sent an item twice but failed to receive a valid response, to make sure we don't create a duplicate record (one with an id, and one with no id).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.