I have a `DialogFragment` that calls a `ViewModel`...
# android
l
I have a
DialogFragment
that calls a `ViewModel`'s method, and the method calls a suspending method via
viewModelScope.launch
. The problem is, when I click OK button on the dialog, I get
kotlinx.coroutines.JobCancellationException: Job was cancelled
error message and the suspending method doesn't work. How can I overcome this problem? I moved the method to my
MainViewModel
and accessed it with
sharedViewModel
, but still the same
JobCancellationException
occured.
r
Does the DialogFragment have its own ViewModel? The lifecycle of the coroutine should be tied to the lifecycle of the dialogfragment
and make sure your only canceling the job, in ViewModel.onCleared
l
I get the same exception whether the DialogFragment has its own ViewModel or using MainViewModel as a shared one. And the coroutine is really simple and there's no canceling code explicitly.
Copy code
fun doSomething() = viewModelScope.launch {
    <http://httpClient.post|httpClient.post><Unit>("<http://example.com>")
}
r
is viewModelScope from Android KTX?
or is that your implementation?
also you should move this conversation to #coroutines
l
OK, thanks.
It's from Android KTX
It was my bad. SharedViewModel works fine.