Leaking Service held by Coroutine

Viewed 130

I'm trying to dive into coroutines world, trying to make some network work in Service (in fact LifecycleService). after starting it I'm calling below method

private fun runConnectionCoroutine() {
    job = lifecycleScope.launch(Dispatchers.IO) {
        try {
            doSomeNetworking()
            if (!isActive) return@launch
            publishResult()
        }catch (e: CancellationException){
        }
    }
}

doSomeNetworking() may take too much time, so user should be able to dimiss this task and Service. I'm calling then job?.cancel() and then stopSelf(). As expected coroutine doesn't stop immediatelly (thats why if (!isActive) return@launch line is there), but Service does and leakcanary detects: CoroutineScheduler$Worker keeps reference to this Service

so the question is: how to properly stop Service during networking coroutine?

0 Answers
Related