Android Volley + Loader pattern?
Asked Answered
L

4

22

I kinda liked Volley framework, but I still have some doubts about it.

For example, how does Volley line up with Loader pattern? Since it's requests are handled in an async manner, calling it on background doesn't make much sense. On the other hand, if we ignore the Loader pattern, we will cancel the loading and reload the necessary resources, it's kinda waste.

How does Volley framework work with Loaders in Android?

Legislative answered 5/6, 2013 at 10:57 Comment(0)
V
12

A Loader can encapsulate practically anything, including Volley requests. When your Loader encapsulates a framework that already handles background work for you and calls you back on the main thread, like Volley, your loader implementation must not inherit from AsyncTaskLoader but simply from the Loader base class. You would then start the Volley request in the onForceLoad() method.

When your loader gets the result back on the main thread through a callback, it just needs to push it to the Activity/Fragment by calling deliverResult().

Your loader would also need to keep a reference to the Volley request in progress to be able to cancel it in onStopLoading(). onStopLoading() is not called in case of configuration change like screen rotation, only when leaving the Activity.

The only disadvantage is that Loaders don't have a built-in mechanism to propagate errors, while Volley does. So in your Volley error callback inside of your Loader, you'll need to either deliver a null result or send a local broadcast to notify the Activity/Fragment of the error.

Viscoid answered 20/4, 2014 at 0:10 Comment(0)
N
4

AFAIK and I have seen in the sources, responses to your requests will be cached, IF the server sends the proper caching headers(ETag), and the second time you will attempt to make a GET request, to the same url, you will be provided with a response from the cache, instead of calling the Network again.(by default Volley caches the requests using as key the URL).

Adding Requests to the RequestQueue should be done from the MainThread, as it makes no sense to call them from a background Thread.

Nasalize answered 20/6, 2013 at 15:34 Comment(3)
I was thinking about a situation where user rotates the device while loading. Normal behaviour of Volley framework would be cancelling requests and re-starting them when orientation change is done.Using Loader pattern would protect the state of loading from orientation changes.Legislative
yes. you are correct. there is no straight, out of the box mean to "re-connect" to a pending request, or "keep it" running and get responses on orientation changes/ Activity Stop-Resume.Nasalize
It makes sense to call it from a background thread in situations such as loading additional information for a push notification before it displays. However, for general purposes it's better to call it from the main thread.Momently
B
3

I just publish an article about Volley and it integration into project over Loader pattern. Advanced approach is shown. Loader states are fully defined and displayed in diagram.

Article: https://plus.google.com/117981280628062796190/posts/8b9RmQvxudb

Loader States Diagram

Buskirk answered 25/1, 2015 at 14:47 Comment(0)
M
1

It is possible to make synchronous requests with Volley via the RequestFuture class. I haven't looked into this personally, but it looks like you could leverage that with a Loader to get the best of both worlds (Volley's Cache with the loading stability of the Loader).

Momently answered 30/8, 2013 at 19:58 Comment(4)
This way your Loader cannot return both intermediate and final results. RequestFuture only returns the first received result (which is intermediate in case you use cache); final result just updates the cache and cannot be propagated to UI.Wisconsin
Good point. At this point, I would not recommend using loaders at all, I think they have too much overhead and it's better to create your own solution from external libraries such as Retrofit or Volley.Momently
There is no need for synchronous requests. If you extend Loader<D> you are expected to do work in background thread anyway.Ineffective
Pius, if you have multiple requests that need to be made, it's much cleaner to do them all in one async thread instead of having to chain callbacks. You actually can't use Volley asynchronously in an AsyncLoader because it throws an error if it's not called from the main thread.Momently

© 2022 - 2024 — McMap. All rights reserved.