GlobalScope.launch {
Log.d("rafa", "thread:${Thread.currentThread()}") // this prints Thread[DefaultDispatcher-worker-1,5,main]
someTextView.text = "new test"
}
Does anyone know why that doesn't crash ^? Even though it's changing text in the UI on a worker thread?
s
streetsofboston
09/07/2019, 3:47 PM
This seems to be an Android 'issue'.
Running UI related code on another thread than the main UI-thread may cause a crash... sometimes it may work fine.... I don't think that all UI methods in Android have a 'is-on-ui-thread' assertion...
➕ 1
a
Adam Powell
09/07/2019, 4:39 PM
No, they don't assert the attached window's UI thread for every operation. WAI. 🙂
r
Rafa
09/07/2019, 6:31 PM
neat. Is there an example in the sdk I can go look at to see that happening for specific UI updates?
a
Adam Powell
09/07/2019, 8:34 PM
Not sure what you mean, views specifically don't check for this
r
Rafa
09/07/2019, 8:39 PM
I have to check my knowledge on this again. I thought in the past when trying to update a view from a background thread, I'd get a crash saying I can't update the UI
s
streetsofboston
09/07/2019, 9:41 PM
There are a bunch of method in the Android SDK that specifically test for being on the main ui thread (one that comes to mind is the
MutableLiveData::setValue
)
But the most just do what they do and, if called on a background thread, may fail in an undetermined way.