Android db request from main thread

I use sql lite and I usually query 1 table. Is it bad if I make a request from the main ui thread?

thanks

+4
source share
4 answers

It depends. If your table is really large, it may take time to complete the query, and this can cause a noticeable lag in your application. In addition, you say that you usually only query one table, so this leaves the possibility of more queries on additional tables.

As a rule, I do a lot of work, for example, requests and loading in the background thread using AsyncTasks, since even if they do not take a very long time, this gives me additional freedom later to expand the application without extensive overwriting.

+8
source

Yes, actually this is bad for a request from the main user interface thread.

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

This is not so bad if it takes less than one or two seconds, but it is always recommended to do it in another thread.

+4
source

Please read the Android Design for Responsive article.

Potentially lengthy operations, such as network or database operations, or expensive computations, such as resizing raster images, must be performed in a child stream (or in the case of database operations via an asynchronous request).

The article describes that performing expensive operations in the main thread can lead to an "Application Error" error, and the OS will kill your application.

Therefore, when you can perform these operations in the main thread, it is better to use background threads.

+2
source

yes, you can indicate that your request will not take much time, another is reasonable to use the Async task for this

0
source

Source: https://habr.com/ru/post/1435118/


All Articles