How many Volley request queues should be maintained?
Asked Answered
Y

1

15

Currently I am maintaining one static Volley request queue as described here:

Instantiating core Volley objects

  private static RequestQueue mReqQueue;

Should there be one and only one static request queue per app? What is the harm in having more than one? for example what if I wanted one request queue just to process twitter requests. And another one for everything else like authentication, image retrieval etc.

Yoohoo answered 17/7, 2013 at 20:13 Comment(0)
L
14

I think Ficus Kirkpatrick said somewhere in his presentation on Volley that optimally, there is only one RequestQueue.

If most of your activities, services and receivers make use of Volley, and you do a lot of switching between them, it makes sense to define a singleton RequestQueue in your Application object so that you don't have to instantiate a new RequestQueue in every acticity / service / receiver onCreate.

However, if you have a lot of activities, and use Volley in only one of them for one request, then you might be better off defining the RequestQueue in just that Activity, or it'll get instantiated in the activities in which you don't use it. This shouldn't hurt functionality, but could hurt memory-wise.


EDIT:

In the volley user group, Ficus said:

RequestQueues are pretty cheap (mostly just threads). We use more than one in our app in order to segregate caches.

Which tells us that it's also a valid use case to use multiple RequestQueues if you need to have separate caches.

Luminosity answered 12/10, 2013 at 10:34 Comment(2)
But what if you switched to another Activity, and want to pause the request in the previouse Activity, load the new request firstly?Brunhild
This is a breach of what an Activity should do. If you want to be able to cancel an ongoing download across Activities, you should perform the download in a Service.Luminosity

© 2022 - 2024 — McMap. All rights reserved.