1

I have a Request in a activity, I need to know if the request is canceled, so I checked in the onResponse() method by using Request.isCanceled(), but it return false. I have no idea when the request is really canceled, and I have read the code, still no answer.

naiyu
  • 745
  • 3
  • 9
  • 25
  • `I want to hide the loading dialog after all the requests canceled, so I need to check if the request is canceled.` if you do not cancel your request it wont be canceled. – mmlooloo Sep 17 '14 at 03:15

1 Answers1

8

The code reveals ALL.

It really depends on the timing.

Once request.cancel() is called, the request is flagged for cancellation and it all depends on what state it's in. There are 4 options:

  1. cancel() is called before cache check - request is discarded by the CacheDispatcher in its run loop (line 94)

  2. cancel() is called, on a request that doesn't have a cached response after the cache check, but before the request is actually performed - request is discarded by the NetworkDispatcher in its run loop (line 102).

  3. cancel() is called after request has been sent, but before a response arrives - ExecutorDelivery discards the response upon arrival (line 92) and DOES NOT NOTIFY THE LISTENER. That means that when onResponse or onError are called, the request has not been canceled, and checking for it there, like you did, will ALWAYS return false.

  4. cancel() is called after delivery - nothing happens, it's too late.

I didn't understand your circumstance exactly, who is canceling the request, and why are you checking, but hopefully that's enough info to help you out.

Itai Hanski
  • 8,540
  • 5
  • 45
  • 65
  • Thanks for heping. Actually I have serveral Request with the same tag in a Activity, and I show a loading dialog before requests execute, now I want to hide the loading dialog after all the requests canceled, so I need to check if the request is canceled. But as your answer, I can't check in the "onResponse()" method. I will try another way, or could you give any opinions – naiyu Sep 17 '14 at 01:01
  • Is there some sort of work around for number 3? Please see my question : http://stackoverflow.com/questions/34577601/how-do-you-cancel-a-volley-request-that-has-already-started – John Ernest Guadalupe Jan 03 '16 at 14:40