Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Loaders be used to access web services?

For what I understand, the Loader framework is geared towards accessing data stored locally in a ContentProvider / SQLite database. We have the CursorLoader class that handles this use case quite well.

But I wonder if it's practical to use the Loader framework to write classes extending Loader / AsyncTaskLoader to access remote web services (e.g. a REST web service)? I always thought that this framework is a bit too rigid and confusing (lack of proper documentation) for this use case. I prefer handling REST calls in more regular way, using AsyncTasks / Services. But recently I've found some articles that used AsyncTaskLoaders and began to wonder.

So why would anyone use Loaders to access Web Services? The only advantage I see here is that Loaders retain their results automatically. There's no Cursor here to manage afterwards.

like image 413
Zsombor Erdődy-Nagy Avatar asked May 12 '13 18:05

Zsombor Erdődy-Nagy


People also ask

What is the importance of loader in Android programming?

Loaders persist and cache results across configuration changes to prevent duplicate queries. Loaders can implement an observer to monitor for changes in the underlying data source. For example, CursorLoader automatically registers a ContentObserver to trigger a reload when data changes.

What are the characteristics of a loader?

Introduced in Android 3.0, loaders have these characteristics: They are available to every Activity and Fragment. They provide asynchronous loading of data in the background. They monitor the source of their data and automatically deliver new results when the content changes.


2 Answers

Realistically, you probably want to use a networking library like Volley. This has some nice features like request batching and image caching. Nonetheless, for the sake of argument lets compare Service, Loaders and AsyncTask.

Services are the way to go if you want to allow the loading to continue while changing Activities or backgrounding your application. Or, if you want to export your service so multiple applications can use it. Otherwise, use a Loader or AsyncTaskLoader.

Loaders have a few advantages over AsyncTasks.

  • They are less likely to cause crashes by executing code after the Activity has finished, since they are aware of the android lifecycle.
  • The design discourages having references to Views or Activities. This reduces the likelihood of forcing the Activity to stay in memory after it has already finished.
  • Monitor the data source for changes and trigger callbacks when they occur
  • They have built in caching that can be useful after rotations. For Cursors, the CursorLoader automatically reconnects at the correct position to the last Cursor loaded

However, they also have disadvantages

  • The API is extremely more cumbersome than AsyncTask. Especially if you care about compatibility with older versions of Android
  • You are already storing UI state inside onSaveInstanceState(), so using the Loader's causes you to save state in multiple ways. This can be confusing to read and understand. Especially if you end up mixing retained fragments into the mix.
  • The Loader caches the loaded result, not the UI state that you actually need

I'm assuming you are just reading from web services, not writing. If you are performing updates to a web service and you need to see the service's response, then this changes things. Using an AsyncTask could prevent you from getting the response if the it is received during a rotation.

like image 149
Brian Attwell Avatar answered Sep 23 '22 07:09

Brian Attwell


There are cases where Loader is suitable for webservices: When your server can send push notifications back to client to notify that data is changed.

like image 40
AVEbrahimi Avatar answered Sep 21 '22 07:09

AVEbrahimi