Should I use a Loader for each http connection or a single Loader for all it enough?

50 views Asked by At

I am newbie of Android, trying to make a simple news feed app. I am using Asynctaskloader for background operations. For now, I am using a single Loader to connect different news sources. My question is should I define and run different loaders for each news source or a single loader will also handle it? I am asking because when the app opens it takes 5-10 seconds to load the news (So far I added only three news source), so could it be because of using a single Loader?

2

There are 2 answers

0
Eddie Lopez On BEST ANSWER

The problem with using a single loader's loadInBackground method to hit different data sources is that such access will be sequential within the associated thread. The total time to fetch and return the news from N sources will be the sum of each one's time, including delays or timeouts, assuming you wait until everything is downloaded to proceed with the presentation.

You should consider more threads/loaders or another strategy (maybe fetch 1, show 1, fetch 2, add 2, etc) where the user doesn't get frustrated with the wait.

0
Show Young Soyinka On

Loaders are good because of its ability to handle life cycle, but it is not as efficient as LiveData and ViewModel. If you care about performance, speed and being latest, use Android Architecture Components (LiveData, ViewModel), also, you don't have to stick to the old system of doing things, you can write multiple simple AsyncTask and wrap it with ViewModel and LiveData. It works like a magic and better than Loaders. For information on how to wrap AsyncTask in LiveData and ViewModel, visit https://medium.com/androiddevelopers/lifecycle-aware-data-loading-with-android-architecture-components-f95484159de4