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

vineethraj49

07/02/2020, 2:01 PM
what's a good way to do things concurrently in
init {}
?
d

Dominaezzz

07/02/2020, 2:02 PM
runBlocking { launch { /* task */} }
?
v

vineethraj49

07/02/2020, 2:03 PM
this for some reason got "stuck" when instantiated from within a Dispatchers.Default
g

gildor

07/02/2020, 2:28 PM
Not the best pattern to have blocking code in init, better to provide asyncronous builder function for it
☝🏼 3
do you have reproduction sample when it “stuck”?
v

vineethraj49

07/02/2020, 2:31 PM
no 😕 I just moved it all to
CompletableFuture
and
.join
and moved on; will try to figure out "where" it got stuck when I get around to it again
g

gildor

07/02/2020, 2:52 PM
why do you need CompletableFuture with coroutines? At least for this case
v

vineethraj49

07/02/2020, 2:53 PM
I don't; wanted to know if there's a "standard" way to do concurrency in init given my
runBlocking
attempt got "stuck"
g

gildor

07/02/2020, 2:55 PM
Those are 2 different problems
“stucked” runBlocking probably means that your code inside has some deadlock, hard to say without sample, but most probably just some bug in your implementations
second problem “how to do something concurrent in init”, you can do that as Dominic suggest, if you want make it blocking, so object cannot be created until operation is finished But imo it’s very bad pattern, object creation shouldn’t block for long time
if create object before operation is finished is fine, just launch a coroutine from init, no need to use runBlocking (I would also probably avoid it, harder to test and adds background tasks, but better then blocking)
if you still want to create object only when all you operations are finished, I would recommend use factory method (even better if it suspend), which does all operations asyncronously in parallel and create object after that and return instance of it It also work well with private constructor and invoke operator in companion object. so it allows you make almost real suspend constructor
o

Orhan Tozan

07/02/2020, 7:40 PM
suspend init { }
g

gildor

07/03/2020, 12:54 AM
I don't think that I buy it, opens all kind compatibility issues with Java and all other platforms Also init block is not a part of public API or signature, but adding suspending init means that class constructor also become suspending, so it will break all non-suspend clients
j

jdemeulenaere

07/08/2020, 9:40 AM
I would suggest to create a
suspend
factory method on the companion object that does all the concurrent stuff needed to initialize your object, e.g.:
Copy code
class MyClass(val param1: String, val param2: String) {
  companion object {
    suspend operator fun invoke(): MyClass {
      return coroutineScope { 
        val param1 = async { ... }
        val param2 = async { ... }
        MyClass(param1.await(), param2.await())
      }
    }
  }
}
Oops sorry @gildor, just saw you suggested the same 🙂
3 Views