I'm trying to cancel IO, when android activity/fra...
# arrow
t
I'm trying to cancel IO, when android activity/fragment gets destroyed. Using coroutines and
lifecycleCoroutineScope
with provided await function seems to work.
Copy code
suspend fun <T> Kind<ForIO, T>.await(ur: UnsafeRun<ForIO>): Either<Throwable, T> = ur.run {
    suspendCancellableCoroutine { continuation ->
        val cancellableAction = this@await.fork(continuation.context)
                .bracketCase(
                        use = { IO.just(Unit) },
                        release = { fiber, exitCase: ExitCase<Throwable> ->
                            when (exitCase) {
                                is ExitCase.Completed ->
                                    fiber.join().attempt().map { continuation.resume(it) }
                                ExitCase.Canceled -> {
                                    continuation.cancel()
                                    fiber.cancel()
                                }
                                is ExitCase.Error -> {
                                    continuation.resume(exitCase.e.left())
                                    fiber.cancel()
                                }
                            }
                        }
                )

        unsafe {
            runNonBlocking(
                    fa = { cancellableAction },
                    cb = { }
            )
        }
    }
}
But I feel I'm missing some more graceful solution. Is there a better way?