Hi All! I'm injecting a "presenter" object into my...
# koin-contributors
s
Hi All! I'm injecting a "presenter" object into my compose views and every time a recomposition happens the presenter is recreated, how could I preserve the first created object? I have read that viewModel{} should work that way but I'm using KMM, so I cannot relay on android VM
r
single {}
s
But single would create a singleton object, I just want the object to persist recompositions, but gets recreated if I enter the same screen later
I guess I could do something with the remember method of Compose
r
guess it needs to be scoped accordingly. i don't use compose tho
m
If you need to create an object that will be “kept in memory” while that scope (a back stack entry) is in memory, you will need to use either ViewModel or a custom solution. You could for example, put the Presenter inside a ViewModel.
If you want a simpler solution (sorry for the self-promotion), you can also use my library for that: https://github.com/marcellogalhardo/retained - it already handle this VM integration + gives you everything a VM would have without the need of inheritance, etc. I created it exactly for the use case you are describing. And it is very easy to combine with any lib, including Koin.
d
You also could use cokoin which also supports scopes https://github.com/burnoo/cokoin
❤️ 1
s
Thanks for both responses, I'll check your library Marcello and I would also consider using Cokoin