Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query db in android from main thread

Tags:

android

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

like image 806
Snake Avatar asked Sep 19 '12 17:09

Snake


People also ask

What is the main thread in Android?

When an application is launched in Android, it creates the first thread of execution, known as the “main” thread. The main thread is responsible for dispatching events to the appropriate user interface widgets as well as communicating with components from the Android UI toolkit.

What is process and thread in Android?

When an application component starts and the application does not have any other components running, the Android system starts a new Linux process for the application with a single thread of execution. By default, all components of the same application run in the same process and thread (called the "main" thread).

How does thread work in Android?

When an application is launched in Android, it creates the primary thread of execution, referred to as the “main” thread. Most thread is liable for dispatching events to the acceptable interface widgets also as communicating with components from the Android UI toolkit.

What are the main two types of thread in Android?

There're 3 types of thread: Main thread, UI thread and Worker thread. Main thread: when an application is launched, the system creates a thread of execution for the application, called main.


3 Answers

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.

like image 195
Raghav Sood Avatar answered Oct 07 '22 06:10

Raghav Sood


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.

like image 43
Jorge Cevallos Avatar answered Oct 07 '22 04:10

Jorge Cevallos


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.

like image 39
Sam Avatar answered Oct 07 '22 05:10

Sam