Hi, my coroutine job immediately gets cancelled ev...
# coroutines
r
Hi, my coroutine job immediately gets cancelled even before staring. Now I am not sure what could be reasons for this. What could be reasons for coroutine to get cancelled even before it is cancelled
Copy code
private fun CoroutineScope?.launchWithNullCheck(
        block: suspend () -> Unit
    ) {
        this?.let {
            Log.d("FUI", "Scope not null")
            val job = launch {
                Log.d("FUI", "executing block not null")
                block()
            }
            Log.d("FUI", "job isActive ${job.isActive}, isCompleted: ${job.isCompleted}, isCancelled: ${job.isCancelled}")
        }
}
s
One possible reason is if the
CoroutineScope
that you’re launching it in contains an already-cancelled
Job
. Launching a job as a child of a cancelled job will immediately return a cancelled job and won’t start the coroutine. Hard to say if that’s what’s happening here, though. Can you give any more information about how it’s behaving? Do you see any output, for example?
r
Launching a job as a child of a cancelled job will immediately return a cancelled job
Let me verify this and give more information
Is there any way to see any cancelled job for the particular scope?
s
this?.coroutineContext?.job
should work (where
this
is the coroutine scope receiver from your example)
r
I printed out whether scope is active or not and existing job is Cancelled or not
Copy code
private fun CoroutineScope?.launchWithNullCheck(
        block: suspend () -> Unit
    ) {
        this?.let {
            Log.d("FUI", "scope isActive: ${this.isActive}")
            Log.d("FUI", "existing job cancelled: ${coroutineContext.job.isCancelled}")
            Log.d("FUI", "Scope not null")
            val job = launch {
                Log.d("FUI", "executing block not null")
                block()
            }
            Log.d("FUI", "job isActive ${job.isActive}, isCompleted: ${job.isCompleted}, isCancelled: ${job.isCancelled}")
        }
}
For me it says job was not cancelled but scope became inactive
Screenshot 2023-03-03 at 15.54.33.png
s
Okay, that explains why the job does not start. The scope’s job is already finished, so it can’t launch new child jobs. If it’s not active, but not cancelled, that means it just completed normally. How did you create this scope?
r
I passed this scope from another class android's viewmodel This is actually
viewmodelscope
and I am holding reference to that in this class
s
In that case I guess it just means that the associated
ViewModel
has already been cleared?
r
Thats the issue, Its not cleared. This happens while the viewmodel is active
s
Okay, I haven’t used
ViewModelScope
myself so I’m not sure know what could cause it to be inactive. Hopefully someone with more Android knowledge can help 🤞
r
I figured it out. Instead of passing
viewModelScope
I was passing its childScope which caused the issue ChildScope was getting completed before my further operations Passing viewModelScope solved the issue
Although while figuring this issue, I found out its best to manage my the coroutineScope for the class (which was receiving coroutine) on its own instead of receiving from another class