Question

What's the easiest way to execute a bunch of requests asynchronously via RoboSpice?

I read somewhere that I need to implement a RequestRunner but I don't know the way to merge it with SpiceManager, Any ideas?

Était-ce utile?

La solution

You can override the num of thread available, defining your own custom SpiceService:

    public class CustomSpiceService extends RetrofitGsonSpiceService {
      /**
      * Overrides the number of threads that will be used to make requests.  The default
      * is 1.
      */
      @Override
      public int getThreadCount(){
        return NUM_THREAD;
      }
    } 

After that you can use your new spiceService in you manager:

    private SpiceManager spiceManager = new SpiceManager(CustomSpiceService.class);

As a bonus, you can detect the the type of your connection, so you can have more threads if you are in a Wifi connection.

/**
 * Overrides the number of threads that will be used to make requests.  The default
 * is 1, so if we are on a fast connection we use 4, otherwise we use 2.
 */
@Override
public int getThreadCount() {

    ConnectivityManager connectivityManager = 
            (ConnectivityManager) DaftApp.getInstance().getSystemService(CONNECTIVITY_SERVICE);

    NetworkInfo info = connectivityManager.getActiveNetworkInfo();
   if(info==null){
       return 2; // there is no network available now. Anyway we use the default num of thread
   }
    switch (info.getType()) {
        case ConnectivityManager.TYPE_WIFI:
        case ConnectivityManager.TYPE_WIMAX:
        case ConnectivityManager.TYPE_ETHERNET:
            return 4;
        case ConnectivityManager.TYPE_MOBILE:
            return 2;
        default:
            return 2;
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top