MobDev
MobDev

Reputation: 1279

Android using a cursor on UI thread

I am wondering when or if its ok to use a cursor to get data from a sqlite database on the ui thread. Basically I use a cursorloader to get a cursor. And then I want to do something with that cursor. Is it ok to do something with the cursor from the main ui thread? Really I just need to read one row from the cursor.

Upvotes: 3

Views: 2467

Answers (2)

Seva Alekseyev
Seva Alekseyev

Reputation: 61388

It's OK. It's not a network operation.

EDIT: by "it's OK" I mean that Android 4.x won't force-close your app for doing that, like if would for performing network I/O on the main thread. Depending on the query time, it might or might not be appropriate to stall the UI thread. For a "get a single row from a single table by its primary key" kind of query, it's perfectly fine. For a hairy query that takes a second or more, consider a background thread and a progress dialog.

Upvotes: -2

Alex Lockwood
Alex Lockwood

Reputation: 83301

Retrieving a row from a Cursor is not an expensive operation... querying the data from the database, on the other hand, could be time-consuming depending on the amount of data you are requesting and/or the complexity of the query. CursorLoaders query for Cursors asynchronously so your approach is fine.

Upvotes: 3

Related Questions