3

I am writing an app that performs a series of requests with Google's Volley API. The problem is that I don't know when the requests have finished. And I need to know if a request has finished before I move further. I can't just wait a certain amount of time with a while loop or sleep/wait method because it seems to halt the thread that the requests are being executed on. Obviously the requests have onResponse callbacks which I can use to mark when the single request is done. But I have an activity that makes multiple requests and I want that activity to be on standby until all requests have been processed. And like I said I can't simply wait/sleep because that makes the request queue sleep. Would I need to write a service that monitors the requests? I'm not really sure how to go about this.

P.S. I'm using a singleton request queue described here.

sadelbrid
  • 411
  • 1
  • 4
  • 16
  • what's wrong to call another request in `onRespnse()` without waiting ? – Zahidul Islam Jun 04 '16 at 18:47
  • Hmm thats actually a smart idea. So you're suggesting instead of enqueuing all requests at once, chain them one at a time via their onResponse callbacks? – sadelbrid Jun 04 '16 at 20:18
  • Yes, So that you can maintain the flow . also, i think you have more control here. Suppose , you want to do 10 request , but , when 4 request is completed then somehow server or internet goes down, That time you can stop calling the other requests and show user a notification that something is not right. – Zahidul Islam Jun 05 '16 at 04:44
  • Were you able to solve this issue? I'm looking for answer to similar problem. Can you please update if you have got the solution working? – user846316 Jul 26 '16 at 14:35
  • Guess the OP doesn't want to share their knowledge – Denny Dec 28 '17 at 21:41

1 Answers1

-1

You could consider using Java's CountDownLatch to accomplish this if the requests don't need to be done serially. See the answers here: How is CountDownLatch used in Java Multithreading? for more details.

Chantell Osejo
  • 1,456
  • 15
  • 25
  • The OP's requests are on the same thread, and this will not work. – SH7890 Oct 28 '17 at 04:50
  • @SH7890, where does it say the requests are on the same thread? Volley handles threading in the background. Just because you make the requests from the same thread doesn't mean they're being performed in the same thread (in fact they can't be if they're called from an activity, or there would be a NetworkOnMainThread exception thrown). – Chantell Osejo Oct 29 '17 at 05:28
  • "because it seems to halt the thread that the requests are being executed on." I take this to mean that they are executed on the same thread, and this lines up with my own experience as well. – SH7890 Oct 30 '17 at 10:30