https://kotlinlang.org logo
#koin-contributors
Title
# koin-contributors
s

sendoav

01/19/2022, 10:06 PM
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

Ryan

01/19/2022, 10:14 PM
single {}
s

sendoav

01/19/2022, 10:41 PM
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

Ryan

01/19/2022, 10:42 PM
guess it needs to be scoped accordingly. i don't use compose tho
m

Marcello Galhardo

01/19/2022, 11:04 PM
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

Damian Zawadzki

01/21/2022, 1:08 PM
You also could use cokoin which also supports scopes https://github.com/burnoo/cokoin
❤️ 1
s

sendoav

01/21/2022, 7:13 PM
Thanks for both responses, I'll check your library Marcello and I would also consider using Cokoin
10 Views