Reputation: 1291
I have an app that, on launch, has to setup a recycler view and, obviously, an adapter. Should I use an AsyncTask on launch? Does it always improve performance? When is suggested to not use it?
Upvotes: 0
Views: 129
Reputation: 4849
Android's main (UI) thread is the one on which many of the familiar activity and fragment lifecycle callbacks are invoked. It's very important to keep the amount of work your app is doing on the main thread to a minimum in order to keep the UI as responsive as possible.
Should I use an AsyncTask on launch?
It depends. What is the size of your data set? Is the data local or on a server somewhere? How long do you suppose it will take to retrieve the data? Will you be receiving more data over the lifetime of your RecyclerView/Adapter? In all but the simplest cases, it's probably safer to put the code that loads the data for an adapter in a background thread. AsyncTask is one way to do this. AsyncTaskLoader is another that provides some additional support for cancellation.
Does it always improve performance?
No. But it should provide a better UI experience for your user.
When is suggested to not use it?
As mentioned before, if you've got a hard-coded static list of items, you're probably fine just loading them and putting them in your adapter. For anything else, I'd suggest loading the data on a background thread (using one of the aforementioned classes.)
Hope this helps.
Upvotes: 3