I'm trying to do a cup heavy calculation and then want to update the UI.
Below is my code:
private fun updateData() {
GlobalScope.launch(Dispatchers.Default){ //work on default thread
while (true){
response.forEach {
val out = doIntensiveWork()
withContext(Dispatchers.Main){ //update on main thread
_data.postValue(out)
delay(1500L)
}
}
}
}
}
is this way of using coroutines okay? As running the entire work on Main also has no visible effect and work fine.
private fun updateData() {
GlobalScope.launch(Dispatchers.Main){ //work on Main thread
while (true){
response.forEach {
val out = doIntensiveWork()
_data.postValue(out)
delay(1500L)
}
}
}
}
Which one is recommended?