Imagine I\'m in a Service that already has a background thread. Can I do a request using volley in that same thread, so that callbacks happen synchronously?
There ar
As a complementary observation to both @Blundells and @Mathews answers, I'm not sure any call is delivered to anything but the main thread by Volley.
The Source
Having a look at the RequestQueue implementation it seems the RequestQueue
is using a NetworkDispatcher
to execute the request and a ResponseDelivery
to deliver the result (the ResponseDelivery
is injected into the NetworkDispatcher
). The ResponseDelivery
is in turn created with a Handler
spawn from the main thread (somewhere around line 112 in the RequestQueue
implementation).
Somewhere about line 135 in the NetworkDispatcher implementation it seems like also successful results are delivered through the same ResponseDelivery
as any errors. Again; a ResponseDelivery
based on a Handler
spawn from the main thread.
Rationale
For the use-case where a request is to be made from an IntentService
it's fair to assume that the thread of the service should block until we have a response from Volley (to guarantee a living runtime scope to handle the result in).
Suggested solutions
One approach would be to override the default way a RequestQueue is created, where an alternative constructor is used instead, injecting a ResponseDelivery
which spawns from the current thread rather than the main thread. I haven't investigated the implications of this, however.