CauCuKien
CauCuKien

Reputation: 351

Should I delete data from table sqlite before update it from server data?

This is the logic which I did on my application when the activity is opened first time :

  1. When Main Activity open , check if network available .
  2. if network is connected, fetch data from server and then save to sqlite. If network is not available, display a toast alert user need to connect network,

And logic for Main Activity open the 2nd time

  1. Check if network is available.
  2. If network is connected, fetch data from server. Now I also want to save new data on sqlite, but the sqlite data is now include old data. Should I delete all old data on table first and then insert new data from server ? if do not delete and go ahead to insert new data into sqlite, the table will duplicate data and how can I resolve ?

Sorry for my English or the question is duplicated. I just am finding a best way to work with data fetch from server. A big thanks if anybody give me a example how to handle it.

Upvotes: 0

Views: 429

Answers (2)

Rahul Tiwari
Rahul Tiwari

Reputation: 6978

Your approach is fairly simple and acceptable if you are dealing with a small data set (say less then 100 values).

However for large data sets consider building some kind of sync mechanism where you ask server only for the difference of data you have locally and data available on server.

one way of doing it is to maintain a sync key.

  1. to start with server will provide you with a key say 0 with all the data.
  2. on next request you will send this key 0 to server.
  3. now sever will check if there are any changes since 0
  4. if yes it will send you delta changes(insert or update) with new sync key (say 1)
  5. if no it will say you are up to date.

in this approach server will have to maintain change history but it will be efficient for mobile client with respect to data base and network.

Upvotes: 1

Ashish srivastava
Ashish srivastava

Reputation: 628

Take a data as a primary key which is unique comes from server end.

Upvotes: 5

Related Questions