2017-11-24 12 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

retrofit2を使用してください。& RxJavaデータフロー処理のリクエスト。

PresenterはAsyncTaskを拡張しないでください。

+1

私はretrofit、rx、またはottoのようなものを使用したくありません。 – Tartar

0

ローダーを使用するのが最善の方法です。見てくださいhere

AsyncTaskLoaderが必要です。古いAsyncTaskと比較して、Loaderには多くの改善があります。

公式文書here。また、Loaderを使用すると、アクティビティのライフサイクルを心配する必要がありません。

関連する問題