Rohan Maity
03/03/2023, 10:06 AMprivate 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}")
}
}
Sam
03/03/2023, 10:10 AMCoroutineScope
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?Rohan Maity
03/03/2023, 10:11 AMLaunching a job as a child of a cancelled job will immediately return a cancelled jobLet me verify this and give more information
Sam
03/03/2023, 10:19 AMthis?.coroutineContext?.job
should work (where this
is the coroutine scope receiver from your example)Rohan Maity
03/03/2023, 10:24 AMprivate 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 inactiveSam
03/03/2023, 10:26 AMRohan Maity
03/03/2023, 10:28 AMviewmodelscope
and I am holding reference to that in this classSam
03/03/2023, 10:29 AMViewModel
has already been cleared?Rohan Maity
03/03/2023, 10:29 AMSam
03/03/2023, 10:32 AMViewModelScope
myself so I’m not sure know what could cause it to be inactive. Hopefully someone with more Android knowledge can help 🤞Rohan Maity
03/08/2023, 10:32 AMviewModelScope
I was passing its childScope which caused the issue
ChildScope was getting completed before my further operations
Passing viewModelScope solved the issue