Which is faster - Cursor or ArrayList?

I have a SQLite database that I have to fetch data from all the time. Changes can be made to the data between each checkout.

My goal is to maximize the performance of my application, so what's the fastest way to do this?

I can imagine 2:

  • constantly opening and closing new cursors

  • query all the data at the beginning and store it in an ArrayList. When the data changes, change the SQLite DB and ArrayList with indexOf


---- EDITED ----

I need data to generate markers on google map.

I considered using CursorLoader, but since I don't need to interact with other applications, I don't want to use Content Providers.

Would it be a good idea to create a custom bootloader?


source to share

3 answers

In short, while it's not always that easy, the fastest way to do it all at once.

Constantly making calls to and from the database can really make it difficult for applications to run, especially if it's on the server and not just your device's SQLite database.

Depending on what you are doing with the data, you might look at something like CursorAdapter

that which handles the display of rows from the database, and every time you insert / update a row, the CursorAdapter updates the ListView accordingly. It also handles opening / closing / moving to the next cursor, making it very clear and easy for developers.

Again, try to make as few calls as possible. If you stick with using ArrayList:

  • Make one call at the beginning for all elements.
  • Loop that cursor and add items to the array list.
  • Use a list of arrays as a cache. Of course, you can update the DB every time you update the list (which might be the safest, IMO), or you can just loop over the list and insert / update / delete when the app exits. If you take this approach, make sure you do it in the type method onPause()

    , as this is one of the earliest methods to kill an activity.


Ideal use case for CursorLoader

. Given the request, it will update your list adapter with the latest data, assuming you are notifying the database changes that have occurred. It also handles activity lifecycle events conveniently for yours (i.e. it closes the cursor when the activity completes, stops updating when paused, etc.).



The fastest way is to explicitly not use the database at all. However, this is clearly not a solution unless you find a way to access your array from elsewhere.

Using a database is a convenient way to centralize data, so many users can access the data and keep the data up to date. Unfortunately, this is the slowest option.

Choosing an intermediate point between speed and availability is a daunting task. You must find a balance between stale data and bandwidth.

If, for example, you were comfortable with displaying data that was only valid 5 seconds ago, then you could probably cache the data locally in your array and arrange for a mechanism to keep it up to date behind the scenes.

If the 5 minute lag was acceptable, you could probably arrange for regular database access.

In addition, whatever mechanism you use must also handle concurrent data changes - perhaps two users are changing the same date at the same time.

You just need to decide where to strike.



All Articles