https://kotlinlang.org logo
#coroutines
Title
# coroutines
p

Patrick

12/13/2019, 10:05 AM
What is the error I am making here? The coroutine should run on the main thread as seen by the log output. However I still get the exception that I can’t use a Toast here.
t

tseisel

12/13/2019, 11:11 AM
I think the logs are misleading here, the coroutineContext does not indicate on which thread you are, unlike
Thread.currentThread()
that clearly indicates that you are running on Dispatcher.Default. if you are not sure you are running on the Main Thread, wrap the Toast code into
withContext(Dispatchers.Main)
.
p

Patrick

12/13/2019, 3:18 PM
I can’t do that because it is not in a suspending function. The only option I have is runOnUiThread(). Does that work the same way or do I have to be aware of other pitfalls as well?
3 Views