Skip to content

Instantly share code, notes, and snippets.

@mazzouzi
Last active June 14, 2022 09:16
Show Gist options
  • Save mazzouzi/a3c8d40717e21cfea0152fd9e52dd5aa to your computer and use it in GitHub Desktop.
Save mazzouzi/a3c8d40717e21cfea0152fd9e52dd5aa to your computer and use it in GitHub Desktop.
class BackgroundSolutionPresenter(val view: BackgroundLeakInterface) {
private val exceptionHandler = CoroutineExceptionHandler { _, exception ->
// Log exception error to server
}
private val coroutineScope = CoroutineScope(Job() + exceptionHandler)
fun doSomeIntensiveWork() {
coroutineScope.launch {
delay(120000) // simulate intensive work
view.onIntensiveWorkDone()
}.invokeOnCompletion {
Log.d("Morad", "${it?.message}")
}
}
/**
* Cancelling the coroutineScope will in turn cancel the coroutine
* which will clear any reference to the fragment -> no memory leak ✅
*/
fun cancelCoroutineScope(message: String) {
coroutineScope.cancel(message)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment