Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArrayList goes empty after restarting app #8

Closed
nightfury96 opened this issue Oct 24, 2018 · 4 comments
Closed

ArrayList goes empty after restarting app #8

nightfury96 opened this issue Oct 24, 2018 · 4 comments

Comments

@nightfury96
Copy link

Hi,
i use below line to add product (data class) to cart in sharedpreference:
var CartItems: ArrayList<ResDataClass.Companion.Product> by bindToPreferenceField(arrayListOf())
when app is alive CartItems has values
but after restarting app CartItems goes Empty
everything except arraylist in sharedpreference has value
something's wrong in saving arrays in sharedpreference
plz fix it
thanks

@yguel
Copy link

yguel commented Mar 28, 2019

Hi,
I have the same issue here for any Mutable collection so far and and any collection stored inside an object.
Is this the expected behaviour ?

@NunciosChums
Copy link

NunciosChums commented Mar 29, 2019

I have same issue and I think same with #2
https://youtu.be/l1I0jeNaVkw

I'm using

var readNoticeIds: HashSet<Int> by bindToPreferenceField(hashSetOf())

@xeropresence
Copy link

xeropresence commented Mar 31, 2019

Yes, it seems that there is an issue with collections.

If you declare

var readNoticeIds: HashSet<Int> by bindToPreferenceField(hashSetOf())

then do

val localCopy = readNoticeIds
readNoticeIds.add(5)
readNoticeIds = hashSetOf()
readNoticeIds = localCopy

It will save, so simply modifying the collection seems to be insufficient to trigger a save

@MarcinMoskala
Copy link
Owner

This is expected behavior as mutable object change does not modify preferences. Use immutable objects. The same problem references all collections that have some persistence or internal structure depending on their state.

data class FullName(
   var name: String,
   var surname: String
)

val person = FullName("Maja", "Markiewicz")
val set = mutableSetOf<FullName>()
set.add(person)
person.surname = "Moskała"
print(person) // FullName(name=Maja, surname=Moskała)
print(person in set) // false
print(set) // [FullName(name=Maja, surname=Moskała)]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants