0

I have a android system that has a local sqlite database which has 4 tables which are related to each other (States -> Sections -> Offices -> OfficeTelephones). I also have a remote server which gets crud operations on it from a panel (website), the android app comes with a basic database which contains some information at first but then the user should be able to sync and updates it's android local sqlite db from the mssql to get the updated, new and deleted data. I know there should be a webservice handling the request but I don't know how to handle the syncing. What is the best approach?

Thanks in advance

arash moeen
  • 4,533
  • 9
  • 40
  • 85

1 Answers1

1

you want to sync your android database with the remote server database,according to me you can use webservices or sync adapter for this. what you can do is everytime user starts the app check for update using the rest apis and update your database according to that if there is any update,but the problem with this approach would be timing to run the apis,if you run it everytime when the app starts if there is a change when the user is using the app then it would not be reflected and if you are polling for updates constantly after a fixed duration,it will use resources and battery power. So the best practice would be to use syncadapters search for it.It is a bit tricky to implement but it will be the ultimate solution.hope that i helped as you mentioned in your comments that user is pressing a button to update the database then you should use timestamps at device when it was last updated and timestamp at the server when database at server was updated by comparing two you get to know that you need to update your android database or not,you can maintain a boolean flag for checking that you need to update a row or not

Pramod Yadav
  • 2,316
  • 2
  • 23
  • 36
  • The syncing is in user's hand, there is a sync button that user can update the application if they want, the app itself has to check if there are changes in the data and inform the user about it. My problem is the server side and the concept to check the differences between 2 database. – arash moeen Nov 12 '14 at 06:46
  • then i think webservices will be a better idea you can use timestamps to compare the database on android and server – Pramod Yadav Nov 12 '14 at 06:47
  • Aha, that's what I'm looking for, could you explain it a bit more? I'll read about it but would be nice to have some explanation here. thanks again – arash moeen Nov 12 '14 at 06:50
  • what you can do is save the time-stamp on device locally when it was last synced and save the time-stamp to server when its database is changed,so you can compare the two and know that do you need to sync data from server or not,i am assuming here that your database transactions in the app are carried out through the rest api – Pramod Yadav Nov 12 '14 at 06:54
  • Yea rest api it is, so basically I'll have a time-stamp for my local db which is the last synced time as you mentioned, then I'll have a time-stamp column on each table which is the last updated time and by comparing them I can filter the new, updated, or deleted data. is that correct or am I missing something? – arash moeen Nov 12 '14 at 06:59
  • yup try it this way,i do not know about the structure of your app so you can go with this way according to your app structure you may need to modify a bit – Pramod Yadav Nov 12 '14 at 07:02
  • it's not really a complex structure, one more question, I can have a field for deleted data to handle the deletion, what about updated data? I don't wanna check each row of the returned data to see if it's there already or not to update it, it is stupid to have an extra field Updated to differentiate it from the new data? – arash moeen Nov 12 '14 at 07:05
  • use a Boolean flag to check that you need to update that row or not – Pramod Yadav Nov 12 '14 at 07:08
  • Thanks man, can you edit your first answer as a sum up to our comment conversation so I can accept it? – arash moeen Nov 12 '14 at 07:09