Hello guys, given a function like ```suspend fun getThree() = withContext(IO) { val one = asy...
d
Hello guys, given a function like
Copy code
suspend fun getThree() = 
  withContext(IO) {
    val one = async { getOne() }
    val two = async { getTwo() }
    
    one.await() + two.await()
  }
would be better to be wrapper in a
coroutineScope
or would the
withContext
be enough?
o
Hello, Davide using a scope you will be protected against possible leaks, because the scope will be attached to your view/viewmodel lifecycle
withContext
is more to switch between dispatchers
in you example, the scope will be handle on where
getThree()
will be called, so I think to switch the dispatcher to IO is fine ๐Ÿ™‚
g
I think itโ€™s wrong explanation from Rafael, in terms of attaching to scope both of them create scope and not different, but withContext also switches scope
using a scope you will be protected against possible leaks
Itโ€™s true, but itโ€™s itโ€™s also true for withContext, because with context is create a scope, same as coroutine scope, but also switch dispatcher
๐Ÿ™ 2
๐Ÿ‘ 1
d
Thank you guys ๐Ÿ™‚
g
so you can think about withContext as coroutineScope + context switching
๐Ÿ™ 1
another question, do you really need withContext(IO) there or not, I see a lot of similar cases, when it really not needed, because getOne() and getTwo() or already asyncronous, or should provide asyn wrapper
๐Ÿ‘ 2
d
Yes, that absolutely makes sense, should take a look into it! Thanks!