SQLLite - Multiple Queries VS Single Query & In-Memory Storage

CodeMonkeyCodeMonkey ✭✭✭GBMember ✭✭✭
edited October 2012 in Xamarin.Android

Hi all

My app has three Activity's which require data from the database including a number of images and some text.

My question: should I be loading the data in from the database every time an Activity starts, or could I load all the data into memory and hold it there for the lifespan of the app?

Adrian

Posts

  • JonathanPryorJonathanPryor Xamurai USXamarin Team Xamurai

    The problem isn't reading the data. The problem is writing the data. You have no control over the process' lifetime; if no Activities are displayed, then Android can nuke your process at any time it wants.

    If you don't need to do any writes, then reading at startup may make perfect sense (unless you have lots of data to read, as that may slow down process startup...).

    Sharing the DB data in-memory between Activities will never make sense if your DB is "huge" and you plan on keeping it all in RAM. You're running on an embedded system: RAM is precious. ;-)

    If you do need to write, for your own sanity you'll want to keep the DB open for short periods of time, write the data within Activity.OnPause() or Activity.OnStop().

    I think that, in general, having data scoped to the Activity makes more sense, as it allows you to flush data at more appropriate times. Combine with ListActvity and some data binding, and you can have just the necessary data read from your DB, not bloating RAM usage by duplicating the DB.

  • CodeMonkeyCodeMonkey ✭✭✭ GBMember ✭✭✭
    edited October 2012

    That's great info, thanks for the reply.

    As the size of the data needed for this app is really small I think I'll go down the in-memory route. I have each Activity loading its data OnCreate() at the moment so will be interesting to see any performance difference across devices using in-memory storage.

  • BryanCostanichBryanCostanich Xamurai USMember, Xamarin Team Xamurai

    Also, @CodeMonkey, we have a great guide here about the activity lifecycle and persisting data: http://docs.xamarin.com/android/tutorials/Activity_Lifecycle

    I highly recommend reading it for a good background on it.

Sign In or Register to comment.