Какой самый простой способ выполнить кучу запросов асинхронно через RoboSpice
?
Я где-то читал, что мне нужно реализовать RequestRunner
, но я не знаю, как объединить его с SpiceManager
. Есть идеи?
Какой самый простой способ выполнить кучу запросов асинхронно через RoboSpice
?
Я где-то читал, что мне нужно реализовать RequestRunner
, но я не знаю, как объединить его с SpiceManager
. Есть идеи?
Вы можете переопределить количество доступных потоков, определив свой собственный 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;
}
}
После этого вы можете использовать свой новый spiceService в своем менеджере:
private SpiceManager spiceManager = new SpiceManager(CustomSpiceService.class);
В качестве бонуса вы можете определить тип вашего соединения, поэтому у вас может быть больше потоков, если вы находитесь в соединении Wi-Fi.
/**
* 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;
}
}