Reputation: 1
I'm making a game of Tetris on android as a project for school and right now im using shared preferences in order to save the current state of the game so that it can be resumed on a later time , i've come to realize that when you store over 100 or so preferences the sharedprefernces object starts working in a strange way , i can save everything but when i try to call the editor to clear (e.clear + e.commit) it wont remove the preferences.
i would appreciate any help regarding this issue
thanks
Upvotes: 0
Views: 74
Reputation: 33495
SharedPreferences
are good and lightweight mechanism how to persist data.
But i think for game it's not a win at all. SharedPreferences
are usually used for persisting non-structured data for example if you have some application that requires login and when User is logged in successfully you can save this state to SharedPreferences
and in next Activities
just check it whether User is logged in or not. But in the game you have (i guess for sure) structured data-structures (for instance players and their properties (values) like reached score, loses, wins etc.).
So i suggest you to think about another mechanism for data persisting. Specifically try to think about a possibility to use classic object serializing or and usage of SQLiteDatabase
which provide more complex solution how to persist structured data.
A main advantage is that you can persist (serialize) whole objects and then simply deserialize them (and not persist them as specific "chunks" in SharedPreferences). Regarding to SQLite
, it provides almost same solution as classic serializing but objects are represented as tables in database.
Upvotes: 1
Reputation: 17429
If you need to remove specific values use this:
SharedPreferences.Editor.remove()
followed by commit()
To remove them all SharedPreferences.Editor.clear()
followed by a commit()
(references here https://stackoverflow.com/a/3687333/1584654).
However If the values remain limitated, for Shared Preferences should not be an issue.
Upvotes: 0