5

I am using sql lite and i am usually querying 1 table. Is it bad if I do the querying from the main ui thread?

Thank you

Snake
  • 14,228
  • 27
  • 117
  • 250

4 Answers4

11

It depends. If your table is really big, it could take time to execute the query, and possible cause a noticeable lag in your app. Also, you say that you usually query only one table, so that's leaves the possibility of more queries on additional tables.

As a general rule, I do a lot of work like querying and downloading in background threads using AsyncTasks, as even if they do not take very long now, it gives me extra freedom later on to expand the app without extensive rewriting.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
4

Yes, actually it is bad to query from main UI thread.

http://android-developers.blogspot.com/2009/05/painless-threading.html

It is not too bad if it takes less than one or two seconds, but it's always recommended to do it on another thread.

Jorge Cevallos
  • 3,667
  • 3
  • 25
  • 37
  • 4
    1 or 2 second lag is not too bad in UI thread? I would so 50-100ms would be acceptable but not best practice since it always can get slower. – Patrick Mar 05 '15 at 16:14
2

Please read the Android article Designing for Responsiveness

Potentially long running operations such as network or database operations, or computationally expensive calculations such as resizing bitmaps should be done in a child thread (or in the case of databases operations, via an asynchronous request).

The article continues to describe that performing expensive operations on the main thread can lead to an "Application Not Responding" error and the OS will kill your app.

So while you can perform these operations on the main thread, it is best to use background threads.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • Thanks for the link. Although I was blockig for more specific answer concerning 1 table db query – Snake Sep 19 '12 at 19:04
  • I'm glad it was useful for you. While you are using one query now, later you might want to do more... I believe it is easier to use best practices from the beginning, this way you will never experience a dreaded ANR and then have to spend time rewriting your code. – Sam Sep 19 '12 at 19:12
0

yes, you can provided your query should not take long time other wise it is better to use Async task for this

Rajendra
  • 1,700
  • 14
  • 17