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.