2017-11-24 4 views
0

샘플 Android 앱에서 MVP 패턴을 따르기 때문에 발표자 클래스를 구현하려고합니다. 내 발표자 구현 내가 발표자 클래스를 구현하기위한 가장 좋은 방법을 찾고 있어요 그리고 난 별도의 클래스에 AsyncTask 이동하고 더 나은 방법이 될 것입니다 더 일반적인 방법으로 그것을 구현 믿고Android MVP : 모범 사례

public class WeatherForecastPresenter extends AsyncTask<Void, Void, WeatherForecast> { 

    private double latitude; 
    private double longitude; 
    private String address; 

    // class that makes sync OkHttp call 
    private WeatherForecastService weatherForecastService; 
    // interface that has callback methods 
    private WeatherForecastView weatherForecastView; 

    public WeatherForecastPresenter(WeatherForecastView weatherForecastView, double latitude, double longitude, String address) { 
     this.latitude = latitude; 
     this.longitude = longitude; 
     this.address = address; 

     this.weatherForecastView = weatherForecastView; 
     weatherForecastService = new WeatherForecastService(); 
    } 

    @Override 
    protected void onPreExecute() { 
     weatherForecastView.toggleRefresh(); 
    } 

    @Override 
    protected WeatherForecast doInBackground(Void... voids) { 
     // gets weather forecast data of given location 
     return weatherForecastService.getCurrentWeather(latitude, longitude); 
    } 

    @Override 
    protected void onPostExecute(WeatherForecast weatherForecast) { 
     weatherForecastView.toggleRefresh(); 

     if (weatherForecast != null) { 
      weatherForecastView.updateUi(weatherForecast, address); 
     } else { 
      weatherForecastView.displayErrorDialog(); 
     } 
    } 
} 

이하하지만 난 할 수 없었다 적절한 해결책을 찾는다. 도움을 주시면 감사하겠습니다.

답변

0

데이터 흐름 처리를 위해 & RxJava 요청시 retrofit2을 사용해보십시오.

발표자는 AsyncTask를 확장해서는 안됩니다.

+1

나는 retrofit, rx 또는 otto와 같은 것을 사용하고 싶지 않습니다. – Tartar

0

가장 좋은 방법은 로더를 사용하는 것입니다. 보세요 here.

AsyncTaskLoader가 필요합니다. 이전 AsyncTask 대 로더의 개선 사항이 많이 있습니다.

공식 문서 here. 또한 로더를 사용하면 활동 라이프 사이클에 대해 걱정할 필요가 없습니다.