Volley or Service with cursor loader

前端 未结 5 494
天命终不由人
天命终不由人 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 10:15

    Volley is just a helper layer for communicating with server that manages threads and stuff. Yes, it already implements nice threading caching and stuff. But you don't have to use it in your Activities. I would even say you shouldn't. We all know that service is in fact the place to do any background network work. It is designed to survive configuration changes and is a natural way for your application to declare it DOES work in background and should be taken with extra care by the system.

    Imagine, there was no Volley. What would you do? I bet you would imlement your threading support in a service, right (we all know Services work on main thread)? It may be as simple as an IntentService, which is a single static worker thread and a handler for it. Or you may go fancy and use ExecutorService to have a pool of threads. Or you can go crazy and start a new Thread() each time in onStartCommand(). Whatever suits your needs and taste.

    So I prefer looking at Volley as just another way of accomplishing this tasks. This time you don't need to do any work with threads yourself, you just have Volley do it for you.

    So the bottom line is use Volley AND Service together.

提交回复
热议问题