Search code examples
androidandroid-syncadapter

Should I use sync adapter


My app requirement

Login.

Signup.

at 1 min periodic interval fetch data from server and update the UI.

no local db.

For Login and Signup I will use normal HTTP call but for periodic request at 1 min interval I was thinking to use SyncAdapter,

should I use SyncAdapter for this ?, even I have no local db storage.

Is it possible to update the UI from SyncAdapter ?

I have gone through the below links How do I use the Android SyncAdapter?

Sync data between Android App and webserver

Android SyncAdapter use case

Sync data between Android App and webserver


Solution

  • Sync adapters are designed to run at off times to keep data current between local and server. Not regularly, and certainly not every minute.

    You should probably just implement a Timer, or Handler to control that.

    Something similar to this answer.

    private int mInterval = 60000; // 60 seconds
    private Handler mHandler;
    
    @Override
    protected void onCreate(Bundle bundle) {
        ...
        mHandler = new Handler();
        startRepeatingTask();
    }
    
    Runnable mStatusChecker = new Runnable() {
        @Override 
        public void run() {
            downloadChangesFromServer();
            mHandler.postDelayed(mStatusChecker, mInterval);
        }
    };
    
    void startRepeatingTask() {
        mStatusChecker.run(); 
    }
    
    void stopRepeatingTask() {
        mHandler.removeCallbacks(mStatusChecker);
    }