Does anyone have the experience to compare the two libraries. And any opinions on this?
a
Arkadii Ivanov
01/17/2023, 8:04 PM
I'm not familiar with KMM-ViewModel, but I can say something about the "obsolete ViewModels" article. It uses Essenty library and it seems that the implementation could be much simpler, given the navigation is not covered in the article. E.g. one can create a shared ViewModel using Essenty library as follows:
Copy code
// commonMain
class MyViewModel : InstanceKeeper.Instance {
override fun onDestroy() {
// Dispose things here
}
}
Using in Android:
Copy code
class MainActivity : AppCompatActivity() {
private val instanceKeeper by lazy { instanceKeeper() }
private val viewModel by lazy {
instanceKeeper.getOrCreate(::MyViewModel)
}
}
Using in other platforms would be close to the following:
Copy code
fun main() {
val viewModel = MyViewModel()
// At some point later
viewModel.onDestroy()
}