Reputation: 3549
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?
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.
Upvotes: 0
Views: 81
Reputation: 1037
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.
Upvotes: 1