0

I have a class SongResponse:

public class SongResponse implements Parcelable {

    @SerializedName("token")
    public String token;

    @SerializedName("items")
    public List<Song> songList;
}

@GET("songs")
Observable<SongResponse> getSongs(@Query("token") String token)

In the manager class, I want to return an Observable by used the request above

public Observable<Song> syncSongs(token) {

    return mSongService.getSongs(token);
        ....

        @Override
        public Observable<Song> call(List<Song> songs) {
            return mDatabaseHelper.setSongs(songs);
        }

}

syncSongs will call another Observer to insert songs in the database.

I used RxAndroid 2. I try to find about .map and .flatMapIterable, but don't result my problem. Could you help me complete this function?

luoihocbk
  • 13
  • 5

1 Answers1

0

You can try something like that :

public Observable syncSongs(token) {
    return mSongService.getSongs(token).flatMap(new Function<SongResponse, ObservableSource<List<Song>>() {
        @Override
        public ObservableSource<List<Song>> apply(@io.reactivex.annotations.NonNull SongResponse songResponse) throws Exception {
            return Observable.just(songResponse.getSongList()); //some changes here
        }
    }).flatMapIterable(new Function<List<Song>, Iterable<? extends Song>>() {
        @Override
        public Iterable<? extends Song> apply(List<Song> songs) throws Exception {
            return songs;
        }
    }).flatMap(new Function<Song, Observable<Song>>() {
        @Override
        public Observable<Song> apply(final Song song) throws Exception {
            return Observable.just(song);
        }
    });
}

Hope this helps.

Cochi
  • 2,199
  • 2
  • 12
  • 15