Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava flatmap: How to make multiple calls based on result from first call

I have 2 retrofit calls I need to make A & B:

(A): returns an ArrayList

(B): gets the result of (A) which is an ArrayList. (B) iterates through the ArrayList and makes a retrofit call using each and combines the resulting data into a final ArrayList that is sent to my subscriber onNext()

I'm having trouble getting the syntax correct....for example I'm trying to return arAllMedia, but my code below expects me to return an Observable.

ServiceFactory.createRetrofitService().getUserFollowing(sessionMgr.getAuthToken())
                .flatMap(new Func1<UserSelfFollows, Observable<? extends ArrayList<Media.MediaData>>>() {
                    @Override
                    public Observable<? extends ArrayList<Media.MediaData>> call(UserSelfFollows userSelfFollows) {

                        //make second requests based on response from First request to get all Users
                        ArrayList<Media.MediaData> arAllMedia = new ArrayList<>();
                        for(UserSelfFollows.UserDataFollows user : userSelfFollows.userdata){

                            Response <ResponseBody> response ;
                            Call <ResponseBody> call;
                            try {
                                call = ServiceFactory.createRetrofitService().getMediaOfUser(user.id,sessionMgr.getAuthToken());
                                response =  call.execute();
                            }catch(IOException ex){
                                return Observable.error(ex);
                            }

                            if (response.isSuccessful()) {

                                try {
                                    String str = responseHelper.streamToString( response.body().byteStream());
                                    Gson gson = new GsonBuilder().create();
                                    Media media = gson.fromJson(str, Media.class);

                                    arAllMedia.addAll(media.mediaData);

                                } catch (IOException e) {
                                    return Observable.error(e);
                                }
                            } else {
                                return Observable.error( new Exception(  responseHelper.getErrorString( response.errorBody().byteStream())) );
                            }
                        }
                        return Observable.just(arAllMedia);

                    }
                })
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ArrayList<Media.MediaData>>() {
                    @Override
                    public final void onCompleted() {

                    }

                    @Override
                    public final void onError(Throwable e) {

                    }

                    @Override
                    public final void onNext(ArrayList<Media.MediaData> arMedia) {

                    }
                })

Using Maxim suggestion below I have below, but it won't compile:

ServiceFactory.createRetrofitService().getUserFollowing(sessionMgr.getAuthToken())
                .flatMapIterable(new Func1<UserSelfFollows, Iterable<?>>() {
                    @Override
                    public Iterable<?> call(UserSelfFollows userSelfFollows) {
                        return userSelfFollows.userdata;
                    }
                })
                .concatMap(new Func1<UserSelfFollows.UserDataFollows, Observable<Media.MediaData>>() {
                    @Override
                    public Observable<Media.MediaData> call(UserSelfFollows.UserDataFollows user) {

                        return ServiceFactory.createRetrofitService().getMediaOfUser(user.id,sessionMgr.getAuthToken());
                    }
                });
like image 745
Mike6679 Avatar asked Nov 09 '22 01:11

Mike6679


1 Answers

you need to return

Observable.just(arAllMedia)
like image 87
Luong Dinh Avatar answered Nov 15 '22 05:11

Luong Dinh