-
Notifications
You must be signed in to change notification settings - Fork 23
How to perform cancellation
Devrath edited this page Jun 15, 2021
·
1 revision
- It is not able to prematurely cancel the calculation because it is not cooperative regarding the cancellation.
- This leads to wasted device resources and memory leaks, as the calculation is not stopped and ViewModel is retained longer than necessary.
- This use case now fixes this issue.
- The UI now also has a "Cancel Calculation" Button. Note: Only the calculation can be canceled prematurely but not the toString() conversion.
class CooperativeCancellationViewModel(
private val defaultDispatcher: CoroutineDispatcher = Dispatchers.Default
) : ViewModel() {
private var calculationJob: Job? = null
fun performCalculation(factorialOf: Int) {
uiState.value = UiState.Loading
calculationJob = viewModelScope.launch {
try {
var result: BigInteger = BigInteger.ZERO
val computationDuration = measureTimeMillis {
result = calculateFactorialOf(factorialOf)
}
var resultString = ""
val stringConversionDuration = measureTimeMillis {
resultString = convertToString(result)
}
uiState.value =
UiState.Success(resultString, computationDuration, stringConversionDuration)
} catch (exception: Exception) {
uiState.value = if (exception is CancellationException) {
UiState.Error("Calculation was cancelled")
} else {
UiState.Error("Error while calculating result")
}
}
}
}
// factorial of n (n!) = 1 * 2 * 3 * 4 * ... * n
private suspend fun calculateFactorialOf(number: Int): BigInteger =
withContext(defaultDispatcher) {
var factorial = BigInteger.ONE
for (i in 1..number) {
// yield enables cooperative cancellations
// alternatives:
// - ensureActive()
// - isActive() - possible to do clean up tasks with
yield()
factorial = factorial.multiply(BigInteger.valueOf(i.toLong()))
}
factorial
}
private suspend fun convertToString(number: BigInteger): String =
withContext(defaultDispatcher) {
number.toString()
}
fun uiState(): LiveData<UiState> = uiState
fun cancelCalculation() {
calculationJob?.cancel()
}
private val uiState: MutableLiveData<UiState> = MutableLiveData()
}