• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Saving complex objects?

I've been stuck on saving complex objects for a little while now... Essentially I have many copies of a conways game of life running in parallel and I am trying to save complex details for each cell and each copy of the game. Each instance of the game might have ~1 dozen unique parameters, and each of its individual cells might have 3-4 dozen unique parameters.

I've looked into SharedPreferences, and that seemed like it might be an ok place to keep a few things, but I might have upwards of 100,000 cells for some instances so it seemed impractical. I didn't want to get into an SQL database for this because it doesn't seem like it would be as fast, but maybe that's what I need? I'm not opposed to it. Right now I'm digging into JSON/GSON and it almost seemed perfect but from what I can tell I might need to recreate each of my objects rather than being able to load them from file? I've been at this roadblock for a few days now and my code is just starting to turn into a giant mess... I'm just not sure what I should be using to try and store this kind of data... please help! xD
 
Your data is complex, and there's a lot of it. This is a task for which a relational database is highly suited. You'll have to design a database schema to represent your objects, and the relationships between them.
Going by your description, you may want the following tables:-

Game
Cell
Cell_Parameters
Game_Parameters

You'll want to use indexes on your tables to optimise lookup.
You may want to initially read in the data from the database, and create the game objects as data structures in main memory, if real time speed is an issue.
 
That's what I thought you might say, LV426... thank you.
Another question though - my biggest aversion to SQLite (using Room I understand is the way to go) is that if I begin updating my database I lose my database? Can you advise me on how to avoid this or to import old database? Thank you very much.
 
You get bonus points for using Room.
The way to do this with Room is to use a Migration class

https://developer.android.com/training/data-storage/room/migrating-db-versions

Btw the traditional way of doing this was to implement the onUpgrade() method of your DB helper class

Code:
public class DataHelper extends SQLiteOpenHelper {
 ...
 
@Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
     ...
   }

You can then write customised code to alter your database in whatever way is necessary for the latest version of your app (preserving existing data of course).
 
Back
Top Bottom