Welcome to the Inaugural Tech Entry for this blog.  Maybe it will be the first of many.  Maybe not.  Or maybe I’ll make you hunt for anything useful here through a mess of self-important ramblings and idiotic brain farts.  Only time will tell.

I’m writing this here because I couldn’t find any word of it anywhere on the internets – which could very well just be for my lack of search prowess.  So I’ll lay it out here.  In case you were wondering:


You may suspect this off the bat, so let me take this opportunity to verify those suspicions.  I ran some simple tests on my first-gen VZW Motorola Droid where I wrote 16kb of data 1kb at a time.  Committing after each 1kb write operation averaged a total write time of ~250ms.  When I delayed the commit until after all the write operations were complete, the total write time averaged ~30ms.

You may be tempted to make a commit to SharedPreferences after any invocation of the Editor.  “Just to be sure.”  This is a sensible notion.  And like all things in mobile, it’s the sensible notions you have to watch out for!  (Just kidding… kind of…)  Committing to shared preferences on single, user-initiated operation makes sense.  But if the commits start getting queued or triggered on a regular basis for whatever reason, your app’s performance can really suffer.

The simplest solution is to remove any offending commit’s and instead call it once in the onPause() event.  This should ensure any pending data to your SharedPreferences structure will be written as your app surrenders control.  I haven’t run into this personally, but I suspect if you’re doing something really crazy with SharedPreferences and it takes an incredibly long time to commit changes you may run into trouble with the onPause() event kicking you out before it’s done.  In which case, of course, something a little more sophisticated is in order.  But I’d say this is a simple enough solution that it’s totally worth a shot and only move on to something more complex if it doesn’t work for you.

Just remember: the more you can limit calls to commit() in your code, the smoother it should run!