Hank
Hank

Reputation: 3497

Android delete all rows or update and remove old?

I need to store a list of contacts from my web server in my android app. The android data is just a copy of the web server and the contacts would have no foreign keys to and from the table. I'm thinking of updating the android data on a periodic basis. But I'm wondering which method would be best to update the data, I'm thinking of:

  1. Delete all rows from the table and insert all the data from the web server
  2. Update the rows that match the webserver and delete those that weren't updated.

Which method is better in terms of performance?

Upvotes: 1

Views: 173

Answers (1)

Eenvincible
Eenvincible

Reputation: 5626

This is what I have done before when I was working on a project:

  • First, truncating the entire table and doing a fresh storage to the database in your app depends on the size of the data you are syncing down. Obviously, it is fine to delete everything if the size is small.
  • The solution I used was have a column in your remote server (corresponding table) that when a contact item is deleted, you update as deleted with a timestamp or just say [deleted].
  • When syncing down to the phone, check to see that you sync ONLY those items that are not deleted or those that were updated/new.Or
  • Sync down the contacts with a JSON field [deleted : true] then once in your android code, check to see if [deleted == true] and consequently delete that contact item from your local db. You simply loop through the list as you store the new items.
  • That means you will be pulling down only NEW or UPDATED items and non-modified items will not be unnecessarily touched. That reduces the amount of transactions you have to do.

Good luck and I hope this helps.

Upvotes: 1

Related Questions