Thursday, 8 September 2016

Making sequential requests using Volley

Volley by nature is async as on completion of request callback gets triggered. We add request by using RequestQueue.add(Request) which gets added in internal priority blocking queue. By default 4 dispatcher or network threads run in background which pick request from queue and process it. As soon as processing is complete dispatcher picks next request in from the queue.

To make volley request sequential we need to restrict dispatcher threads to single thread instated of four. By doing this we restrict Volley from executing next request before complication of current request.

We can use RequestQueue constructor
* Creates the worker pool. Processing will not begin until {@link #start()} is called.
* @param cache A Cache to use for persisting responses to disk
* @param network A Network interface for performing HTTP requests
* @param threadPoolSize Number of network dispatcher threads to create
public RequestQueue(Cache cache, Network network, int threadPoolSize) {