pumpkee
pumpkee

Reputation: 3357

Callback if Volley RequestQueue is done with all it's tasks?

I am using the volley networking library for android. I'am looking for a way to get notified when a bunch of requests are finished, rather than checking all the response listeners individualy (which i also do). Is there an easy way to get a callback from the queue when all tasks are done?

Upvotes: 4

Views: 6589

Answers (2)

inaps
inaps

Reputation: 1664

I've found another way for me. It works excellent.

RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
  final AtomicInteger requestsCounter = new AtomicInteger(0);

  for (String data: someArray) {
    requestsCounter.incrementAndGet();

    queue.add(new StringRequest(
      Request.Method.GET,
      "https://stackoverflow.com",
      response -> {
         ...some stuff for response
      },
      error -> {
         ...catch error here
      }
  ));

  queue.addRequestFinishedListener(request -> {
      requestsCounter.decrementAndGet();

      if (requestsCounter.get() == 0) {
          ...all requests are done
      }
  });
}

Upvotes: 1

Vrashabh Irde
Vrashabh Irde

Reputation: 14367

Keep the requests count in a member variable and decrement everytime a request finishes, and when the counter goes to 0, you're done! I'm not aware of a callback exposed that tracks all the requests and comes back at the end.

int requestPending= 0;
for(int i=0;i<numberOfRequests;i++)
{
    requestQueue.add(request);
    requestPending++;
}
// For each requestQueue item finished onResponse received, do requestPending --

Upvotes: 5

Related Questions