In one of my recent SO questions, someone suggested that I use Loader
for my solution. So here I am trying to understand how to implement a simple AsyncTaskLoader
Here's what I've come up with:
public class Scraper extends AsyncTaskLoader<List<Event>> {
List<Event> lstEvents;
public Scraper(Context context) {
super(context);
}
public List<Event> loadInBackground() {
//This is where I will do some work and return the data
}
}
That's all I've figured out. I read the docs for the AyncTaskLoader
but I have never come across anything so cryptic and messy like it. There's a million methods, all of which are contrary to each other and looking at them it is imossible to deduce, the order in which they are invoked or even whether they should be overridden and invoked. The life-cycle of this task is a damned nightmare.
What I'm looking for is to simply scrape some data and return it. I'd also like to store it in a class variable so that I return it the next time promptly without having to scrape all the data again.
I have no open cursors, streams or anything like that, just a simple variable called lstEvents
(which might be big). I don't want to leak memory and waste resources so I'll be glad if someone could explain what I need to close/nullify where and when to get this task working.
Where should I store the data into the class variable? Shoudl i do it at the end of my loadInBackground
method or should I do this in the deliverResult
method?
In this simple scenario, are there places that I really need to check whether the task was cancelled or whether it was reset or should I simply not override these methods and let the AsyncTaskLoader
handle it.
Some scaffolding code would be helpful if anyone knows. Thanks a ton.
In one of my recent SO questions, someone suggested that I use Loader for my solution.
That probably was not a good suggestion.
The life-cycle of this task is a damned nightmare.
Not to mention that the Loader
contract suggests that you are supposed to be able to dynamically detect changes in the data and deliver updates automatically to the one using the Loader
, which is easy for a ContentProvider
, difficult for other on-device stores, and not especially practical for scenarios like yours.
Frankly, I'd just use an AsyncTask
and call it good.
That being said, you might take a look at the Loader
implementations I made for SQLite databases (sans ContentProvider
) and SharedPreferences
as examples of custom loaders. The only methods you
need to implement are loadInBackground()
and onStartLoading()
. You may need deliverResult()
if the built-in implementation is insufficient -- in your case, I suspect that the built-in one will be just fine. You can add in some of the other methods, as you will see in my AbstractCursorLoader
, though once again I suspect that you will not need them.
are there places that I really need to check whether the task was cancelled or whether it was reset or should I simply not override these methods and let the AsyncTaskLoader handle it.
Start with letting AsyncTaskLoader
handle it, and worry about them only if you determine that you need them for one reason or another.