-2

I am new to Android development and I am creating an app for practicing purposes, my app only retrieves data from the webserver and copies it into the SQLite database, but how do I know if there is new data that I don't have in my local DB or it has been updated? Is there any way to tell Android "update this records and insert this new ones else if there aren't any do nothing"?

Is the creation of a syncadapter a must or I can do it with JobScheduler or Firebase JobDispatcher?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Otto Cheley
  • 552
  • 9
  • 20
  • You could switch to Realm Sync or Couchbase Mobile (if you don't want to actually use Firebase) which have synchronization built in. Neither are SQLite, though. – OneCricketeer Mar 20 '18 at 07:07

3 Answers3

3

How do I know if there is new data that I don't have in my local DB or it has been updated?

Well, there can be 2 mechanism for your app to know if there is new data which you need to fetch from the remote server and save to local sqlite database. Either Push or Poll.

Now, which one to use would depend on the situation.

Polling - Lets say user is looking a list of items. The users pulls to refresh, then you might choose to make a call to server to check if there is any new data. Or you may choose to continuously poll for new data continuously, which might not be a great idea since it might consume resources which is unnecessary.

Pushing - When server has a new data, then it can send an FCM push notification with the appropriate data. You can handle the push notification in your app and update the database with the new data. For example, if you have one table only in your local db, you would have unique Id's for each row. You can include the id and other relevant information in the FCM message and update the corresponding row in your local db.

Decent read: Poll vs. Push - Any reasons to avoid Push Notifications?

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
2

There are 2 phases:

  1. Using Push Notification to notify you application when there is a new piece of data. This will help sync data in real-time.
  2. Your database and the server database should have some sign to detect if there is a new record. For instance, you can use an increment id, if the local id is smaller than the server's one, it is time to retrieve updated data.This will help sync data when users are off for a long time and then use the app again.
Cao Minh Vu
  • 1,900
  • 1
  • 16
  • 21
  • And what about if I update a record in webserver database, how could I be able to detect that specific record changed and needs to be updated in the local database? – Otto Cheley Mar 20 '18 at 07:09
  • For the first phase, it is quite easy, just send a push notification to notify client with the id of the changed record. For the second phase, you can add a flag to all records. If it is false, there is no changes. If it is true, there is a change. When there is a request to update from client side, this flag will be reset to false. – Cao Minh Vu Mar 20 '18 at 07:13
0

Syncadapter are meant for these operations only, it's mechanism suits the best with your scenario.

And for your query:

Is the creation of a syncadapter a must or I can do it with jobScheduler or Firebase JobDispatcher?

You can refer this answer

Aseem Sharma
  • 1,673
  • 12
  • 19