Last active
June 14, 2022 09:16
-
-
Save mazzouzi/a3c8d40717e21cfea0152fd9e52dd5aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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