Volley or Service with cursor loader

前端 未结 5 487
天命终不由人
天命终不由人 2021-02-01 09:49

I almost always use a Service when I download data from a web service. I store the result in a database and displays the result in my view using a cursor loader. But after Googl

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-01 09:52

    I encountered the same issue, and I didn't like to have separated processes for local and network request. I extended the Volley library to include the process of having a local database request (for offline content) and a network request.

    Basically I have a CompleteRequest that will return:

    • an intermediate response, which can be either from http cache or from local database (run in parallel, the fastest that finishes return a result, the second one is ignored)
    • a final response, which will be from the network (or an error)

    The request looks like:

    public class SampleRequest extends CompleteRequest {
    
        public SampleRequest(int method, String url, ResponseListener responseListener, Response.ErrorListener errorListener) {
            super(method, url, responseListener, errorListener);
        }
    
        @Override
        protected Object getLocalResponse() {
            // query your local database for example
            // return the result or null if there is no result from database
            return new Object();
        }
    
        @Override
        public void saveNetworkResponseToLocal(Object response) {
            // save the network response to the local database
            // next time the request is performed the local response will return the result faster than the network request
        }
    
        @Override
        protected BallResponse parseBallNetworkResponse(NetworkResponse response) {
            // parse the result from the network request, in the same way than with volley
            return Response.success(new Object());
        }
    }
    
    
    

    And you perform the request like that, with the response listener that includes the intermediate and the final response:

    mRequestQue.add(new SampleRequest(Request.Method.GET, "http://some.url", new ResponseListener() {
        @Override
        public void onIntermediateResponse(Object response, BallResponse.ResponseSource responseSource) {
            // intermediate response, such as from local database or soft cached network response
        }
    
        @Override
        public void onFinalResponse(Object response, BallResponse.ResponseSource responseSource) {
            // final response, which is the network response
        }
    
        @Override
        public void onFinalResponseIdenticalToIntermediate(BallResponse.ResponseSource responseSource) {
            // final response is identical to intermediate one
            // happens when intermediate is from soft cache and network response is identical (not modified)
        }
    
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // network response is an error, in the same way than with volley
        }
    }
    ));
    
    
    

    It's still in an early stage of development, but I'm using it in several apps and it works nice. I wrote a quick readme with examples and there is sample project that can help.

    You can check it out at https://github.com/lukaspili/Volley-Ball

    提交回复
    热议问题