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
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.
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).
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With