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?
Volley Uses Caches To Improve Performance In Android: Volley uses caches concept to improve the performance of App. For example, lets say you are using Asynctask to fetch image and description from a JSON array created in server API.
To achieve it without using any patterns or other libraries, you can mark the request as finished if it responded, and call the method, in each of them, you want to execute if all the requests are finished. On that method, you just need to check if all the requests are done.
requestQueue is used to stack your request and handles your cache. You need to create this RequestQueue in your application class or in a Singleton class. Then only you can use the same requestQueue from multiple activities.
Android Volley NetworkImageView is designed in such a way that it creates an image request, sends it and even displays the image, after it is downloaded from the URL.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With