Completable job for loop instead of delay runs until loop ends #5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello Mitch!
I experimented with coroutines and found that immediate cancellation only works if there is a delay function in the coroutine. In other words, if we replace the delay with a lengthy(250000 instead of 100) for loop, it will continue to run until the loop ends at 250000 (you can see it in the log.d) and then run into the cancellation phase. I think it only good for garbage collection, not for immediate cancellation.
The cause why delay function works is this code:
return suspendCancellableCoroutine sc@ { cont: CancellableContinuation ->
cont.context.delay.scheduleResumeAfterDelay(timeMillis, cont)
}
This code suspends/delays the Coroutine. While suspended the cancellation is immediate. That's why it works.
There is another branch with my solution of immediate cancellation. I make a pull request tfor that also.
Thx
Gabor