Hi, I'm playing around with <KMM-ViewModel> librar...
# multiplatform
m
Hi, I'm playing around with KMM-ViewModel library and I noticed one issue. Shared ViewModel used in the iOS application does not cancel its CoroutineScope. It is problematic when we launch long running operations which should be canceled when ViewModel is cleared. Invoking the
viewModel.onCleared()
from Swift takes no effect and shared coroutine keeps running. I checked the source code of the library and I noticed that
KMMViewModel
in the
appleMain
source set has empty
onCleared
implementation. On the other hand the same class but int he
otherMain
source sets cancels the Coroutine scope in
onCleared
. Do you know guys if this is an intentional implementation? If yes what is a recommended way to cancel this scope when given ViewModel is no more used by the iOS application?
r
Hi. Yes that is intentional. The CoroutineScope is actually cancelled inside the ViewModelScope. In a future version this will be moved to the
onCleared
function, but that shouldn’t make a difference in the behaviour. The library will automatically cancel the CoroutineScope as soon as the Swift wrapper is deallocated. Could you share some more details about your applications? If the scope isn’t cancelled that probably means the application is still keeping a reference to the viewmodel (wrapper).
m
Hi Rick, thanks for pointing it out that cancellation happens inside the ViewModelScope. I missed this fact. I assumed that iOS app has to call the
onCleared
method explicitly and this is how I've performed my experiment. Didn't thought that scope is cancelled automatically when the reference is lost. Thanks for the answer and great work with this library kodee loving
👍🏻 1