简体   繁体   中英

What is faster - a Cursor or ArrayList?

I have an SQLite database which I have to be constantly retrieving data from. Changes may be done to the data between each retrieval.

My goal is to maximize the app performance, so what is the fastest way to do this retrieving?

I can imagine 2:

  • constantly opening and closing new cursors

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

---- EDITED ----

I need the data to create markers in a google's map.

I have considered using CursorLoader but as I don't need to interact whith other apps I don't want to use Content Providers.

Would creating a custom loader be a good idea?

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

Constantly making calls to and from a database can really make your apps performance bottleneck, especially if it's to a server and not just your devices SQLite database.

Depending on what you're doing with the data, you may be able to look into something like a CursorAdapter which handles the display of rows from the database, and each time you insert/update a row, the CursorAdapter will update the ListView accordingly. It also handles the opening/closing/moving to next of the Cursor, making it very readable and easy for developers to follow.

Again, however, try to do things in as few calls as possible. If you stick to using an ArrayList:

  • Make one call in the beginning for all items.
  • Loop through that cursor and add items to an array list.
  • Use the array list as a cache. Sure, you could update the DB each time you update the list (which might be safest, IMO), or you can just loop through the list and insert/update/delete when the app closes. If you take that approach, make sure you do so in a method like onPause() , as it is one of the earliest methods in which an Activity can be killed.

Perfect use case for a CursorLoader . Given a query, it'll keep your list adapter up to date with the latest data, assuming you notify when changes happen in the DB. It also conveniently handles activity lifecycle events for your (ie. it'll close the cursor when the activity finishes, stop updating when it pauses, etc.).

The fastest way is obviously to not use a database at all. However, that is clearly not a solution unless you find some way of exposing your array to access from elsewhere.

Using a database is a convenient way of centralising the data so many users can access the data and have the data up-to-date at all times. Unfortunately this is the slowest option.

Choosing your middle-ground between speed and availability is a difficult task. You have to find a balance between stale data and throughput.

If, for example, you would be comfortable with a picture of the data that was valid just 5 seconds ago then you could probably cache the data locally in your array and arrange for some mechanism to keep it up-to-date running behind the scenes.

If a 5 minute lag was acceptable you could probably arrange for a regular push to database.

Also, any mechanism you use must also handle parallel changes to the data - perhaps two users change the same datum at the same time.

You just need to decide on where to strike your balance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM