Guilherme Lima Pereira
04/23/2021, 3:44 PMDeferred
, launched on MainScope
, should I be able to await
until it finishes? I mean, should it block the Main Thread
(that’s my goal, even though it sounds weird)?Tijl
04/23/2021, 3:46 PMTijl
04/23/2021, 3:46 PMGuilherme Lima Pereira
04/23/2021, 3:49 PMGuilherme Lima Pereira
04/23/2021, 3:50 PMTijl
04/23/2021, 3:51 PMrunBlocking
Guilherme Lima Pereira
04/23/2021, 3:53 PMTijl
04/23/2021, 3:55 PMGuilherme Lima Pereira
04/23/2021, 3:57 PMephemient
04/23/2021, 4:27 PMephemient
04/23/2021, 4:28 PMephemient
04/23/2021, 4:29 PMGuilherme Lima Pereira
04/23/2021, 6:02 PMrunBlocking {
Log.d("GLP", "Started runBlocking")
val result = withTimeoutOrNull(2_100) {
Log.d("GLP", "Started withTimeoutOrNull")
envVar.fetch().join()
Log.d("GLP", "Finished withTimeoutOrNull")
}
if (result == null) {
Log.d("GLP", "Reached timeout")
}
Log.d("GLP", "Finished runBlocking")
}
Guilherme Lima Pereira
04/23/2021, 6:03 PMenvVar.fetch()
returns a Deferred<Unit>
(from a async
call)Guilherme Lima Pereira
04/23/2021, 6:04 PMTijl
04/23/2021, 8:51 PMrunBlocking
on the main thread works just fine, only runBlocking (Dispatchers.Main
on the main thread is problematicephemient
04/23/2021, 8:52 PMTijl
04/23/2021, 8:57 PMTijl
04/23/2021, 9:01 PMDispatchers.Main.immidiate
does work from inside runBlocking
, e.g.
runBlocking { // called from Main
withContext(Dispatchers.Main.immidiate) {
}
}
I presume the same goes for other .immediate
versions of Dispatchersephemient
04/23/2021, 9:01 PMephemient
04/23/2021, 9:05 PMTijl
04/23/2021, 9:12 PMrunBlocking
, even from a suspended context. E.g. inside init{}
or lazy{}
or get/set
. Of course you should know what the code you call is doing but I think that rule generally should apply.
For this reason, I hope they make immediate
usable from runBlocking
, it’s only a lack of implementation this does not work right now.ephemient
04/23/2021, 9:35 PMdewildte
04/23/2021, 11:47 PMTijl
04/24/2021, 9:24 AMrunBlocking
.
Still saying “be careful with using runBlocking, because it will block your thread” is better than “don’t use runBlocking if you might be called from a suspend context”. It simply goes waaay too far in my opinion. Especially the maybe there, but even then.
Essentially coroutines have a dual purpose, they provide suspending functions, but they also are context scheduler. I end up using runBlocking here and there to schedule work in the correct context, rather than doing a suspended wait for some long running task.ephemient
04/25/2021, 2:18 AMGuilherme Lima Pereira
04/26/2021, 1:27 PM850ms
and not 2100ms
.
@dewildte we created an internal SDK that returns a map. The goal of this map is to allow not initializing some SDK we know it’s crashing (e.g. what happened with Facebook). This map comes from a server and it should be fast (everything was optimized on it on server side, latency, cache, etc), but we won’t trust it, that’s why we’re putting an 850ms
withTimeout
block.
Why runBlocking
?
I want to block the app initialization (Application class) until the fetch timeout. In the happy path, we would be able to not call the initialize function of some libraries we’ve wrapped.
Does it make sense for you? Please, share your sincere opinion 😄Guilherme Lima Pereira
04/26/2021, 1:30 PMrunBlocking
call on our project, we’ve talked about it and it didn’t make sense until now.
I’d be very happy if you have any idea on how implement it without runBlocking
dewildte
04/26/2021, 1:49 PMdewildte
04/26/2021, 1:51 PMGuilherme Lima Pereira
04/26/2021, 2:03 PMApplication
, so dealing with it on a Splash screen won’t work for me, I think…Tijl
04/26/2021, 2:09 PMephemient
04/26/2021, 2:23 PMephemient
04/26/2021, 2:24 PMGuilherme Lima Pereira
04/26/2021, 8:06 PMrunBlocking
(dealine x time to refactor) but your question was very important. I thought about it and I’ll organize a project to move some initializations to the first activity and then I can run things in parallel in there, without blocking the threadTijl
04/26/2021, 8:20 PMGuilherme Lima Pereira
04/26/2021, 9:07 PM