简体   繁体   中英

RxJava - How to get Single from nested method

In my Presenter i have a method which gets some list from DataHolder:

disposable.add(dataHolder.getMonthOfAttractions(monthInAdvance)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(new DisposableSingleObserver<Map<String, List<Attraction>>>() {
                @Override
                public void onSuccess(Map<String, List<Attraction>> stringListMap) {

                }

                @Override
                public void onError(Throwable e) {

                }
            }));

Then, in my DataHolder I'm checking if my list isn't null. If true, returns my list, if false it downloads this list from server :

public Single<Map<String, List<Attraction>>> getMonthOfAttractions(int monthInAdvance) {
    Map<String, List<Attraction>> monthOfAttractions = monthlyAttractionsMap.get(monthInAdvance);
    if (monthOfAttractions != null)
        return Single.fromCallable(() -> monthOfAttractions);
    else
        return apiGetMonthOfAttractions(monthInAdvance);

The problem is with apiGetMonthOfAttractions method. I dont know how to correctly implement this method to return value to my Presenter.

I've tried something like:

private Single<Map<String, List<Attraction>>> apiGetMonthOfAttractions(int monthInAdvance) {
    cnkRetrofitProvider.getApiInterface().getAttractions(monthInAdvance)
            .subscribeWith(new CnkApiObserver<AttractionListResponse>() {
                @Override
                public void onSucceeded(AttractionListResponse result) {
                    result.getMonthOfAttractions();
                }

                @Override
                public void onFailed(Error error) {
                }
            });
}

But in this case i have "missing return statement" and I'm out of ideas how to implement it. I'm begging to learn RxJava, so be understanding.

Please help :)

EDIT: This is what how my Retrofit getAttractions() method looks like:

public interface CnkApiInterface {

@GET("pl/front-api/{dateFrom}/{dateTo}")
Single<AttractionListResponse> getAttractions(@Path("dateFrom") String dateFrom, @Path("dateTo") String dateTo);}

This is what you are after:

private Single<Map<String, List<Attraction>>> apiGetMonthOfAttractions(int monthInAdvance) {
    return cnkRetrofitProvider.getApiInterface()
            .getAttractions(monthInAdvance)
            .flatMap(attractionListResponse -> Single.just(attractionListResponse.getMonthOfAttractions()));
}

I thing just try to do something like (it only depends what does your cnkRetrofitProvider.getApiInterface().getAttractions(monthInAdvance) returns)

private Single<Map<String, List<Attraction>>> apiGetMonthOfAttractions(int monthInAdvance) {
  return cnkRetrofitProvider.getApiInterface().getAttractions(monthInAdvance)

}

should do the trick

You can always just map the result to List<Attraction> so @wojech_maciejewski s answer still holds, you jast need to add a mapping function.

private Single<Map<String, List<Attraction>>> apiGetMonthOfAttractions(int monthInAdvance) {
  return cnkRetrofitProvider.getApiInterface().getAttractions(monthInAdvance)
                            .map(atractions -> /* convert to List<Attraction> here */) 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM