I'm using Volley to connect to my REST API in my Android application and for some activities, I want to take some action only after all my requests have finished. In JavaScript, for those familiar with promises like in AngularJS, I would do:
$q.all([
resourceA.get(),
resourceB.get(),
resourceC.get()
])
.then(function (responses) {
// do something with my responses
})
How can I do something like this with Volley? I know I could have the ResponseListener callbacks check against some integer that counts the requests that are pending, but this seems like a hack. Is there a simpler way to do this?
You can use CountDownLatch.
It's a special object that blocks the current thread until it's own internal count goes to 0.
As it is blocking the current thread, you have to execute it in a separate thread (or in a service if you are sending your Volley Request from a service).
Implementation example :