Allan Wang
12/29/2018, 3:36 AMsuspendCancellableCoroutine
and to check for isCancelled
whenever the method can be cancelled and then to throw an exception or call resumeWithException
? Is there any other way in doing this?gildor
12/29/2018, 4:38 AMAllan Wang
12/29/2018, 9:44 AMfun getList(): List<Int> {
val list = mutableListOf<Int>
list.add(/* computed value */)
// If cancelled return list
list.add(/* computed value */)
// If cancelled return list
...
return list
}
In the example above, I would wrap everything in suspendCancellableCoroutine
and call the continuation, since that’s the only way I know how.
In another situation, I might want to propagate a cancellation exception when the current block is incomplete but cancelled. Here, I can still use the continuation pattern, but I’d rather not create my own exception to resume with if there’s another way. My assumption is that by cancelling a job, a cancellation exception should already exist somewhere, so I shouldn’t have to make another one.gildor
12/29/2018, 10:43 AMAllan Wang
12/29/2018, 6:02 PMbdawg.io
12/29/2018, 8:12 PMAllan Wang
12/29/2018, 8:16 PMbdawg.io
12/29/2018, 9:17 PMyield
in any suspend fun
at points of cancellation suspend fun doLongWork() {
repeat(10) {
doSomethingOne()
yield() // check if it should cancel or be re-dispatched
}
doSomethingTwo()
yield()
...
doSomethingN()
yield()
doFinal()
}
Allan Wang
12/29/2018, 9:19 PMgildor
12/30/2018, 3:04 AMAllan Wang
12/30/2018, 3:12 AMgildor
12/30/2018, 3:16 AMAllan Wang
12/30/2018, 3:27 AMsuspendCancellableCoroutine
, returning a default value isn’t equivalent to resumeWithException
is it?withContext
? A lot of my checks are to ensure that my android context is still valid before executing something that uses it on my main thread. Eg:
attempt = launch(<http://Dispatchers.IO|Dispatchers.IO>) {
try {
val data = parser.parse(FbCookie.webCookie)
yield()
withContext(Dispatchers.Main) {
loading.dismiss()
createEmail(parser, data?.data)
}
} catch (e: Exception) {
createEmail(parser, "Error: ${e.message}")
}
}
gildor
12/30/2018, 7:19 AM