audax
08/21/2025, 7:38 AMaudax
08/21/2025, 1:19 PMdeinit {}
blockFrançois
08/21/2025, 2:23 PMFrançois
08/21/2025, 2:31 PMdorche
08/21/2025, 8:08 PMFrançois
08/22/2025, 6:29 AMonCleared
is important as it’s the only way to know/do the ViewsModel has been destroyed correctly.
> - what is your exact use case for the clear() function?
We can’t call directly the onCleared, as this is an internal method, and the only way is using the ViewModelStore like Compose does.
The ViewModelStore clear()
function calls the ViewModel onCleared
method and do some cleaning stuff (like cancellation).
> Wouldn’t the viewmodel automatically be destroyed when your ViewController/SwiftUI is destroyed and stops referencing it?
Yes, but the viewModelScope
won’t be canceled correctly, and other stuff linked behind the KMP ViewModel.François
08/22/2025, 6:33 AMdeinit
event, NOT onDisappear
event, the KMP ViewModel should do the same on SwiftUI/UIKIt.audax
08/22/2025, 7:50 AMdorche
08/22/2025, 8:00 AMFrançois
08/22/2025, 8:09 AMViewModelStore
, there is no such thing on SwiftUI.François
08/22/2025, 8:10 AMdorche
08/22/2025, 8:12 AMFrançois
08/22/2025, 8:12 AMFrançois
08/22/2025, 8:14 AMFrançois
08/22/2025, 8:16 AMdorche
08/22/2025, 8:32 AM@State private var viewModel = ViewModel()
would be destroyed/cleared and also doesn't cover any async work happening in the VM. Granted that could be a bit easier with iOS's Task API but 🤷François
08/22/2025, 9:25 AMFrançois
08/22/2025, 9:29 AM