Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot invoke observe on a background thread

I just upgraded to AndroidX and after doing that all my request. I make in background thread do not run but come up with this error.

private void getContactsList() {
    Task.callInBackground((Callable<Void>) () -> { 
     mainActivityViewModel.geContacts(contactListRequest).observe(MainActivity.this, new Observer<ContactListResponse>() {
            @Override
            public void onChanged(@Nullable ContactListResponse contactListResponse) {
                if(contactListRequest != null){
                    System.out.println("Successful");
                }
            }
        });
        return null;
    }).continueWith((Continuation<Void, Void>) task -> {
        if (task.isFaulted()) {
            Log.e(TAG, "find failed", task.getError());
        }
        return null;
    });
}



java.lang.IllegalStateException: Cannot invoke observe on a background thread
    at androidx.lifecycle.LiveData.assertMainThread(LiveData.java:443)
    at androidx.lifecycle.LiveData.observe(LiveData.java:171)
    at com.qucoon.rubies.MainActivity.lambda$getContactsList$12(MainActivity.java:887)
    at com.qucoon.rubies.-$$Lambda$MainActivity$qPAxeGqyWT-wT3M7e8stM1rX2gQ.call(lambda)
    at bolts.Task$4.run(Task.java:357)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)
like image 658
QuCoon Team Avatar asked Feb 15 '19 00:02

QuCoon Team


2 Answers

You need to execute your code in the main thread try to call getContactsList() via a handler:

Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        public void run() {
            getContactsList()
        }
    });
like image 69
Hamza Wakrim Avatar answered Nov 15 '22 20:11

Hamza Wakrim


If for Kotlin and Coroutines (with AndroidX lifecycle library 2.2.0), the following might do:

lifecycleScope.launch {
   withContext(Dispatchers.Main) {
      // Do something
   }
}

Alternatively if not for lifecycleScope and the Lifecycle 2.2.0 library, you can define your own CoroutineScope and use it as so.

like image 5
benChung Avatar answered Nov 15 '22 20:11

benChung