Volley - http request in blocking way

前端 未结 5 1391
时光说笑
时光说笑 2020-12-09 04:13

I\'m learning how to use Google Volley these days. It\'s very convenient for fast networking. It seems that all the requests are running in background in Volley. For example

5条回答
  •  情深已故
    2020-12-09 04:50

    I want to add something to Gabriel's answer. While RequestFuture blocks the thread from which it is called and it serves your purpose, the network request itself is not carried out in that thread. Instead, it is carried out on a background thread.

    From what I understand after going through the library, requests in the RequestQueue are dispatched in its start() method:

        public void start() {
            ....
            mCacheDispatcher = new CacheDispatcher(...);
            mCacheDispatcher.start();
            ....
               NetworkDispatcher networkDispatcher = new NetworkDispatcher(...);
               networkDispatcher.start();
            ....
        }
    

    Now both CacheDispatcher and NetworkDispatcher classes extend thread. So effectively a new worker thread is spawned for dequeuing the request queue and the response is returned to the success and error listeners implemented internally by RequestFuture.

    So I see no point in making a separate blocking thread to use RequestFuture. Instead as Makibo mentioned in his answer - " use a callback listener onSuccess (SignUpResponseListener in that case) and put the code there."

提交回复
热议问题