Reputation: 862
I have an Android application with an SQLite DB with several tables that reflect several types of objects. I am now and have in the past loaded the SQLite data to ArrayLists before presenting the data to the user in list views. Pretty much load all the required data into objects, hold them in the memory and check to make sure no duplicates are added. I share the objects in an Application object so I can access them across separate activities.
I'm wondering if using a loader and creating a content provider for the list is a better approach. I think it would make the code simpler in the long run. The advantage of a loader is that it is on the fly and on another thread right? I'm not too concerned with speed because I have only a few rows to load, but it may be easier for a loader and content provider than managing the DB and temporary ArrayLists as I have to do now.
Anyone have have any suggestions? Again, speed is not a huge problem, but would using loaders be recommended and simpler? Thanks.
Upvotes: 2
Views: 1065
Reputation: 8641
The real advantage of a Loader is that it doesn't hang your Activity while the query is running. Running a query within an Activity is a no-no; while you wait for a response from the db you're blocking the UI thread. Depending on the size of the query result (not just the number of rows), this hang can be bothersome to users.
A Loader, on the other hand, runs on a separate thread.
In addition, it automatically gives you a ContentObserver, so that you can reload your data and view whenever the data changes. I can't tell if this will conflict with your caching scheme.
Upvotes: 4