2014-03-01 3 views
1

RoboSpice을 통해 여러 요청을 비동기 적으로 실행하는 가장 쉬운 방법은 무엇입니까?RoboSpice로 병렬 실행

나는 RequestRunner을 구현해야하지만 어쨌든 SpiceManager과 병합하는 방법을 알지 못합니다. 어떤 아이디어가 있습니까?

당신은 당신의 자신의 주문 SpiceService 정의 가능한 스레드의 납입을 대체 할 수 있습니다

답변

5

: 당신은 관리자에 새 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; 
     } 
    } 

을 : 보너스로

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; 
    } 
} 
+0

좋은 답변입니다. 또한 UI 스레드에 대해 적어도 하나의 코어를 비워 두십시오. 따라서 RS에 할당 된 이상적인 코어 번호는 장치의 코어 수와 최대한 동일해야합니다 (1). – Snicolas