2017-12-12 14 views
1

私は簡単なObservableを持っています。Rxjava2、Retrofit2 - 2つのクラス間でデータを転送します。

class Manager { 

List<City> mList = new ArrayList(); 

public Observable<List<City>> getCitiesObservable(String countryId) { 
// I want to update mList value in each new request; 
return CitiesApi.getCities(countryId); 
} 

セカンドクラス:

public interface CitiesApi { 
    @GET("location/cities") 
    Observable<List<City>> getCities(@Query("country") String countryId); 
} 

また、私は二つのクラス持っているあなたが見ることができるように

class Presenter { 

    public void request() { 


    Manager.getCitiesObservable("us") 
      .subscribeOn(Schedulers.newThread) 
      .observeOn(AndroidSchedulers.mainThread) 
      .subscribe(new ......) 
     } 

を、私はそれぞれの新しい要求にmList値を更新したい」コメントを書き込みました" Presenterがリクエストを行うたびにManagerクラスでmListを更新できますか?

+0

あなた 'Serialize'あなたがそのリクエストから取得応答をしましたか? –

+0

どういう意味ですか? –

+0

私はそれが私のやり方で答えを投稿します、多分あなたはよく理解します –

答えて

2

あなたが何をしたい達成するために(mapのような)RxJavaの変換演算子を使用する必要があります。ここでは、すべてのオペレータのリストだ - ReactiveX - Operators

そして、ここでは、あなたがそれを行うことができます方法は次のとおりです。

class Manager { 

    List<City> mList = new ArrayList(); 

    public Observable<List<City>> getCitiesObservable(String countryId) { 
     return CitiesApi.getCities(countryId) 
       .subscribeOn(Schedulers.io()) 
       .map(new Function<List<City>, List<City>>() { 
        @Override 
        public List<City> apply(List<City> cities) throws Exception { 
         // Do your stuff and return a List<City> object 
        } 
       }); 
    } 
} 
+1

ありがとうございます。できます。 –

0

[OK]を、ここで私が約束したものです:

は、私は、このようにそれを行うには、アダプターを設定し、mListを用意し考えてみましょう。 要求を呼び出し、関数がここで変数

private void makeGetMyChatsRequest(String searchWord){ 
    RestClient.getApi().getChats(searchWord).enqueue(new Callback<Chats>() { 
     @Override 
     public void onResponse(Call<Chats> call, Response<Chats> response) { 
      if (response.isSuccessful()) 
       if (response.code() == 200){ 
        chatsDatas = response.body().getChats(); 
        chatAdapter.notifiDataSetChanged(); 
       } 
     } 

     @Override 
     public void onFailure(Call<Chats> call, Throwable t) { 
      Log.d(TAG, "-=onFailure=-\n" + t.getMessage()); 
     } 
    }); 
} 

にレスポンスボディを置く私の場合には、改造インタフェース

@GET(RestClient.API_GET_CHATS) 
Call<Chats> getChats(@Query("username") String username); 

そして、それが応答するオブジェクトのtypeある<Chats>

public class Chats implements Serializable { 
private ArrayList<ChatsData> chats; 

public ArrayList<ChatsData> getChats() { 
    return chats; 
} 

public void setChats(ArrayList<ChatsData> chats) { 
    this.chats = chats; 
} 
} 

私はデータの配列を持っていたので、この配列内のデータの種類も定義しました。 l要求呼び出し関数の変数chatsDatasに渡されます。

public class ChatsData implements Serializable { 
private int chat_room_id; 
private int read; 
private int user_id; 
private String name; 
private String picture; 
private int is_online; 
private LastMessageData last_message; 
//here will be getters & setters for each field 
} 
+0

ありがとうございますが、私はrxjavaでやりたいと思います。私はクラスマネージャがデータを受け取り、プレゼンターは –

+0

を受け取ることを願っていますが、リストを更新するというアイデアは同じでなければなりません。新しいデータを取得してリストに再設定します。 –

+0

私は理解します。しかし、あなたは1つのクラスでそれを行います、もし20人以上のマネージャーがいたらどうしますか?あなたのコードは普遍的ではなく、rxなしで見えます。 私は私のクラスマネージャは、この リターンCitiesApi.getCities(countryId).doOnNext(CLASSマネージャでSAVE DATA)のような類似するであろうことを望みます。 –

関連する問題