https://kotlinlang.org logo
Title
i

ivano

10/11/2018, 5:03 PM
what is the best practice strategy in kotlin/android to save an array and use it after?
a

adam-mcneilly

10/11/2018, 5:04 PM
after what?
This seems like it's not a kotlin specific question. You should try stackoverflow or the Android United slack for things like this. If you meant after the app closes, you should look into sharedpreferences or room databases.
👆 1
i

ivano

10/11/2018, 5:07 PM
my case is I have setData(ArrayFromRetrofit) and need to have a new and old instance because I am using a DiffUtil that compare old and new array retrieved from Retrofit to compare them and detect changes. So I would like to retain the state of the first array, because I lose the scope/status when I do a retrofit call
(maybe I could use a simple CompanionObject? as kind of singleton? or what?)
a

adam-mcneilly

10/11/2018, 5:09 PM
I guess it depends on what you're worried about losing state. If the fragment never dies but you're just making two network calls, you can cache it in the fragment. A companion object sticks around as long as the application does (for the most part). If you want to persist across sessions you'd need shared prefs or a DB.
i

ivano

10/11/2018, 5:10 PM
so I guess a companion object is the most idiomatic way to go
thank you very much Adam!
a

adam-mcneilly

10/11/2018, 5:11 PM
Of course! I don't wanna be a heckler, but we try our best to keep this chat kotlin specific, so please check out Android United. I'm also always happy to chat about Android via DM but I'm not always on line. 🙂
i

ivano

10/11/2018, 7:29 PM
really kind of you for the community
thanks
I am gonna check android united next time, is that sometimes you have idomatic choices