0

In my android app I has long network operation. After operation is finished I need to update my ui.

So as result long operation need to execute in background thread.

Snippet:

private val isShowProgressLiveData = MutableLiveData<Boolean>()

  // launch a new coroutine in background and continue
GlobalScope.launch() {
            try {
                val executeOperations = OperationFactory.createExecuteTraderOperation(Trader.Operation.CREATE, base, quote)
                val response: Response<Void> = executeOperations.await()
                if (response.isSuccessful) { 
                    isShowProgressLiveData.value = false
                    isForwardToTradersLiveData.value = true
                } else {
                    Debug.w(TAG, "doClickStart_error")
                }
            } catch (e: Throwable) {
                Debug.e(TAG, "doClickStart_network error: $e.message", e)
            }
        }

But I get error on

isShowProgressLiveData.value = false

Error message:

java.lang.IllegalStateException: Cannot invoke setValue on a background thread
    at androidx.lifecycle.LiveData.assertMainThread(LiveData.java:461)
    at androidx.lifecycle.LiveData.setValue(LiveData.java:304)
    at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
    at com.myoperation.AddTraderViewModel$doClickStart$3.invokeSuspend(AddTraderViewModel.kt:55)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:238)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)

I need to update my ui in main thread. So how I can fix this?

Alexei
  • 14,350
  • 37
  • 121
  • 240

2 Answers2

2

You can switch to main thread like with the withContext(Dispatchers.Main) When you fire a coroutine with launch will start on the Dispatchers.Default. The best is to specify it like this:

GlobalScope.launch(Dispatchers.IO) {
            try {
                val executeOperations = OperationFactory.createExecuteTraderOperation(Trader.Operation.CREATE, base, quote)
                val response: Response<Void> = executeOperations.await()
                if (response.isSuccessful) { 
                  withContext(Dispatchers.Main){ //switched to Main thread
                    isShowProgressLiveData.value = false
                    isForwardToTradersLiveData.value = true
                 }
                } else {
                    Debug.w(TAG, "doClickStart_error")
                }
            } catch (e: Throwable) {
                Debug.e(TAG, "doClickStart_network error: $e.message", e)
            }
        }

EDIT: Or you can just use .postValue() instead of .value or .setValue() and forget about withContext().

coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58
0

You're still in background thread when you try to execute

isShowProgressLiveData.value = false

You can do something like this:

GlobalScope.launch() {
        try {
            val executeOperations = OperationFactory.createExecuteTraderOperation(Trader.Operation.CREATE, base, quote)
            val response: Response<Void> = executeOperations.await()
            if (response.isSuccessful) { 


                YourActivity.runOnUiThread(object:Runnable() {

                override fun run() {
                isShowProgressLiveData.value = false
                isForwardToTradersLiveData.value = true
                    }


             }
            } else {
                Debug.w(TAG, "doClickStart_error")
            }
        } catch (e: Throwable) {
            Debug.e(TAG, "doClickStart_network error: $e.message", e)
        }
    }