https://kotlinlang.org logo
Title
d

dorf

10/18/2020, 7:57 AM
is it common practice to pass around a coroutine context? I come from the C# world, and I’m trying to figure out how to actually call suspend functions
e

ephemient

10/18/2020, 8:27 AM
kotlin.coroutines.CoroutineContext
is pretty low level, you generally don't need to think about it
kotlinx.coroutines.CoroutineScope
uses
kotlin.coroutines.CoroutineContext
as a building block for structured concurrency; there's other users too
but assuming you're working with
kotlinx.coroutines
, about whether to pass a
kotlinx.coroutines.CoroutineScope
or not, see https://medium.com/@elizarov/coroutine-context-and-scope-c8b255d59055
g

gildor

10/18/2020, 8:54 AM
To call suspend function you need a suspend block, the easiest way to create is use any of default coroutine builders, such as launch/async/runBlocking
u

ursus

10/18/2020, 4:33 PM
for me I divider objects into those who have state and scope, and those who "do" things the first own their own coroutine scope and the latter just has suspend functions, no state, no dispatchers, just pure suspend functions the first objects own the second types
g

gildor

10/18/2020, 11:25 PM
Not sure that it's correct separation. It's completely fine for suspend function to has blocks with scopes and dispatchers inside
u

ursus

10/19/2020, 12:15 AM
well, different view, worked for me. Repos do stuff. ViewModels / Managers own state/scope/disposables