Hello everyone, tricky coroutine context question,...
# coroutines
y
Hello everyone, tricky coroutine context question, will appreciate any help. I have 2 levels in my app:
Core
:
Copy code
CoroutineExceptionHandler {...} + SupervisorJob() + Dispatchers.Default
UI
:
Copy code
coreContext + (several custom elements holding activity reference) + Dispatchers.Main.immediate + SupervisorJob(coreContext[Job])
Let’s say I have
Copy code
// core level
class CoreModel(coreScope: CoroutineScope) {
  suspend fun operation(): Result {???}
}

// ui level
uiContext.launch { 
  showResultInUi(coreModel.operation())
}
What is the correct way to implement
operation()
to run it on
Copy code
coreContext with job from current coroutine (to be cancelled when UI context is cancelled) and without custom elements (to prevent activity leaks)
? Ideally core level should know nothing about custom element’s keys.
g
Just wrapping content of operation with withContext(coreScope.coroutineContext) should be enough for this case
👍 1
Cancellation will work when uiContext.launch is cancelled
Do you really need core context in this case? Because it wouldn't cause any leak of activity in any case