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)
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()
}
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With