RSS Feed

A Tale of Cursors, Adapters and Loaders

Posted on Friday, June 15, 2012 in Coding, User

Warning, This is a rant!! I wrote this when I was experiencing some trouble with loaders. I have since fixed the issue, but I will still post this just for the (rant) record. By all means, use loaders if you are developing Android applications with lists. They improve user experience, and software developers should not strive to make their own lives easier, but rather that of the users.

Lists are central to mobile application development. If you take a look at the vast majority of apps, there is a list somewhere in there buried. Whether it be a playlist, task list, list of songs, list of contacts, list of messages, list of preferences, it is fair to say that at some point in the lifetime of every mobile application, there will be a need for lists.

Writing list views (or list activities) used to be a relatively simple matter on the Android platform. You had to simply implement an adapter to your database, create a cursor, hand it over to your activity using startManagingCursor() and then set the adapter on your list. Every now and then, when stuff changes, you will let the adapter know with notifyDataSetChanged(). Life was good.

Then came the loaders. You see, the previous method made the CursorAdapters load the list data on the UI thread. For the applications which show only lists of small amounts of data, this was not a problem. However, doing long running stuff like loading large amounts of data items on the UI thread is not a good idea. This can lead to non-responsive applications or bad user experience and I get it, nobody likes that.

Along came the loaders to the rescue. They will simplify your life and make everything smooth by running an asynchronous task to load the data for the list. This will happen in a background thread and your UI will get notified when the data is ready.

So my application is running fine with the old method and I decide, hey, instead of using a bunch of deprecated methods and non-recommended practices, why not switch to the new methods and use loaders. It will make everything better and more peachy, after all, loaders are the best thing since sliced Cupcake, right?

Wrong! First of all the amount of code required just to display a simple list of items is enormous. You have to implement a custom cursor loader, or if you want to use the provided CursorLoader, you have to write a ContentProvider for your database. That seems to me like overkill for applications with no ambitions for sharing their data with others.

But hey, writing code is fun right? This is what we do after all. So I forge ahead and get to the fires of Mount Doom and I get burned (what was I thinking). Now my cursor will not refresh when the fragment is removed and replaced. I have no idea why. I’ve tried all I know and asked the question on StackOverflow. Hopefully someone smarter than me will figure it out and help me find the way back to the Shire.

I’ll let you know if the eagles pick me up or if I perish behind the gates of Mordor. This story is not finished yet.…..

Update: The eagles did come Smile

Share and Enjoy:
  • Twitter
  • Google Bookmarks
  • Digg
  • Facebook

Bring on the comments

  1. […] you read that right. I am using asynchronous loaders. After our little fight, we had a nice discussion and now all is good in loader land […]

  2. Brett says:

    I imagine you are aware of this, but for others who may land on your post, be sure to check out Mark Murphy’s CWAC-LoaderEX library. Allows you to use loaders without a content provider! Thanks Mark for simplifying our lives!

Leave a Reply