class DeepLinkPresenter(private val deepLinkView: DeepLinkView) { private val job = Job() private val scopeIO by lazy { CoroutineScope(job + Dispatchers.IO) } private val scopeUi by lazy { CoroutineScope(job + Dispatchers.Main) } fun handleDeepLinkData(appLinkData: Uri) { scopeIO.launch { try { // stuff val result = processDeepLink(appLinkData) // stuff } catch (exception: Exception) { scopeUi.launch { deepLinkView.showInfo("There was an error processing the message. ${exception.message}") } } } } fun clear() { scopeUi.coroutineContext.cancelChildren() scopeIO.coroutineContext.cancelChildren() } }