Issue
I am creating application that need to work in offline. so I need to sync between server database and local database. I already write the script to do sync between server and local using AsyncTask but sometime it fails to execute the script immediately (delayed) and I am not sure if asynctask could be executed although the application is closed .
THE TASK :
User request new data to server --> server provide the new data in JSON (and also send firebase push notification to other phones to do sync --> AsyncTask/IntentService process the data in user phone (insert/update)
WHEN DO I NEED TO EXECUTE THE TASK?
- everytime I press save button.
- When user receive firebase push notification with type "sync" (I already write this script). (Script need to run whether application is closed or opened and without click the notification, the notification is unclickable and just information sync task running)
so my question is should I use AsynTask or IntentService for that purpose?
**any tutorial about syncing with IntentService with retrofit would be a great help for me.
Solution
Out of IntentService and AsyncTask, IntentService is better suited for this purpose. AsyncTask cannot live on its own and needs to be started from an Activity or a service. Also, in this case, you do need to change the UI on task completion and hence AsyncTask is out of the question.
For data sync, you can also look into WorkManager/JobScheduler. It has nice to use APIs and will provide you with better sync options like : Sync when connected to WiFi, or sync when connected to charger etc.
Answered By - Udit
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.