What is the advantage of loaders over Asynctask in Android?
Asked Answered
P

6

11

Are there any advantages of Loaders over Async task? Also, how to make loaders compatible for phones with Android froyo.

Edit:

The primary problem here is that I'm not using the native DB(SqlLite). Using the DB on development server. Obviously, I can't use CursorLoader any more. AsyncTaskLoader has no examples at all. If any, please do link.

Is it a better idea to load the data required onto the local DB and then query it using CursorLoader?

Pastorship answered 31/1, 2012 at 10:39 Comment(1)
Maybe you are not implementing native DB inside of your app. But are you querying data on the device itself (i.e. music, photos, contacts, calendar, etc.)? If so, then you can still use CursorLoader.Ninette
H
10

Yes, Loaders are more advantageous than AsyncTask as they take care of a lot of things that AsyncTask falls short of, miserably.

  1. Screen Orientation changes are difficult in AsyncTask. I used to have such a problem, till I used an Activity Control class, which I used to retain while configuration changed. I can give you some code if you want to know how. The app used to crash, though, when you changed the orientation multiples times even before the entire data loaded. The secret here is not load a lot of data with your first thread and and finish your threading tasks as soon as possible. Even if it happens in the background, Android has a shabby way of dealing with threads. You never know when one of your tasks would be killed.

  2. Even if you use a AsyncTaskLoader, makes sure that you use an Activity manager. This will help you in getting more control over the activites and AsyncTask.

Yes, it is compatible in all the old version of Android. You need to include the support library(Most of the times, this is included by default but its always nice to double check.)

Hypostatize answered 31/3, 2012 at 16:56 Comment(1)
+1 AsyncTasks can get nasty. See thisFinalism
B
1

For one, loaders are easier to code (they're almost built-in in Fragments). Loaders (specifically CursorLoader) also handles your cursor for you (like the deprecated manageQuery).

Check out this link to read on how to use Loaders pre-Honeycomb.

Bering answered 31/1, 2012 at 10:46 Comment(0)
B
1

There simpler to implement and take care of a lot of the life cycle management which previously had to be done "by hand" with AsyncTasks. See the answer to this question for further detail.

With regards to using them with Froyo, they're available via the compatibility library.

Bowdlerize answered 31/1, 2012 at 10:51 Comment(2)
The problem here is I am not dealing with a native DB. Querying it from development server. How is that possible using CursorLoader?Pastorship
Just noticed you updated your question, have you looked at the AsyncTaskLoader example here developer.android.com/reference/android/content/…Bowdlerize
A
1

It seems no one is talking about the disadvantages of loaders! I'm currently working on a system that runs other services in the background.

What I have noticed is that as soon as a screen with a loader is resumed. The cursor used by the loader locks up the DB.

It may not be open to most people but getDatabaseWriter from sqlite is actually a synchronized method and hence the cursor used by the loader is never closed until the loader is reset or terminated thus locking up access to the DB.

I cannot recommend using a loader under these circumstances nor can I advice using a loader when your result set consists of less than 100 items which are static and never seem to change.

Archilochus answered 28/1, 2014 at 12:2 Comment(0)
W
0

Another advantage with loaders is that they handle screen turn event gracefully whereas asynctask can give you trouble.

Whoopee answered 31/1, 2012 at 11:3 Comment(0)
R
0

Biggest diff:

CursorLoader will update your UI's content as soon as its related ContentProvider changes its content(say through a Service), while AsyncTask will only update your UI when you tell it.

Rapt answered 21/10, 2013 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.