Allan Wang
04/22/2019, 6:13 AMrunBlocking
safe for production if we have helper suspended functions, and are using an interface where it’s okay to block the thread?
For instance, I’m building a notification widget, which requires updates to be done in onDataSetChanged
, before the provider will apply the datagildor
04/22/2019, 6:47 AMAllan Wang
04/22/2019, 7:25 AMsuspend fun loadData(): Data
which is suspended purely to ensure that it never gets called on the main thread.
You now want to use an interface containing
fun a()
fun b()
where you must load the data in a
. Internally, b
must execute after the data is loaded.
In this case we could make two copies of the data loading function, where one is suspended and one is not. I’m curious if that is necessary or if we can just wrap it in runBlocking
gildor
04/22/2019, 7:32 AMwhich is suspended purely to ensure that it never gets called on the main thread.This is really strange case. Why do you need suspend function for this? suspend means that function is async
where you must load the data inStill strange for me, can you show example.. Internally,a
must execute after the data is loaded.b
Allan Wang
04/22/2019, 7:53 AMgildor
04/22/2019, 7:58 AMIs it not a common pattern to specify suspend purely to ensure thread values?What exactly do you mean? To switch to main thread?
launch
, it will work for 99% casesserebit
04/22/2019, 2:35 PMgildor
04/22/2019, 4:19 PM