2017-06-14 17 views
0

私は2つのAPIメソッドを持っています。各要素のAPI呼び出しをrxjavaのリスト呼び出しと組み合わせる方法

@POST("/api/photos/") 
Single<List<Photo>> getPhotos(); 

@POST("/api/user/profile") 
Single<Profile> getProfile(@Query("photoId") String photoId); 

私はいくつかのRX操作と、このAPIメソッドを組み合わせた後>>>プロフィール、シングル<一覧<ペア<写真を返すようにしたいです。出来ますか?どうやって?

答えて

2

これは

public class TestCombining { 
    public Single<List<Pair<Photo, Profile>>> test(ApiInterface apiInterface) { 
     return apiInterface.getPhotos() 
       .flattenAsObservable(photos -> photos) 
       .flatMap(photo -> apiInterface.getProfile(photo.getId()) 
         .map(profile -> new Pair<>(photo, profile)).toObservable()) 
       .toList(); 
    } 

    interface ApiInterface { 
     Single<List<Photo>> getPhotos(); 

     Single<Profile> getProfile(String photoId); 
    } 

    interface Profile { 
     String getPhotoId(); 
    } 

    interface Photo { 
     String getId(); 
    } 
} 
私の解決策であります
関連する問題