1

I understand that I cannot access a MySQL DB from the main thread (UI thread). So I know now that I have options for doing so. I can use a worker thread ( Thread t = new Thread() ) to access MySQL db and get what I need or I can do AsyncTask to access MySQL db and get what I need.

But my question here is, what would be the right way of going about this or the most efficient approach here? I really want to understand and follow good programming procedures when I come across this.

So retrieving data from a MySQL db: AsyncTask, a worker thread or would there be no difference time, power etc?

JoeyL
  • 1,295
  • 7
  • 28
  • 50

3 Answers3

4

Actually the best way would be to use Loaders. More specific a CursorLoader. The advantage of the Loaders compared to AsyncTasks and Threads is that the Cursors will be tied to your Activity or Fragment lifecycle. So you will not end up in trobule if your AsyncTask or Thread finishes after your Activity or Fragment are stopped, and try to notify a component that is no longer present.

You can find some examples for Loaders here How to Use Loaders, and here Life Before Loaders.

Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
4

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask. (Straight from the doc).

http://developer.android.com/reference/android/os/AsyncTask.html.

An alternative to asynctask for long running operations is robospice.

https://github.com/octo-online/robospice

Update:

Asynctask is deprecated using coroutines or any other threading mechanism. Consider suing work manager for defferable jobs.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • IDK why you say that AsyncTask should be used for "a few seconds at most"... nevertheless for very long operations it is recommended to use `Services`. – Ovidiu Latcu Mar 26 '13 at 08:03
  • you will be running long operations in doInBackground() andu cannot update ui untill doInBackground finishes, keep the user waiting to interact with ui. Only in onPostExecute() ui is updated and user can interact with ui. – Raghunandan Mar 26 '13 at 09:39
  • you can use `publishProgress` to update the `UI` when using an `AsyncTask`. – Ovidiu Latcu Mar 26 '13 at 10:05
  • @Ovidiu Latcu agreed. what if the user wants to interact button on the screen, other than publishing update say to the progress bar.This method publishProgress is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field. – Raghunandan Mar 26 '13 at 10:14
2

AsyncTask is better compared to Thread

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

And go through this Handler vs AsyncTask vs Thread

Community
  • 1
  • 1
Nirali
  • 13,571
  • 6
  • 40
  • 53