首页 > 解决方案 > android java 2 observable,单

问题描述

我正在尝试通过在 costructor 中为该位置传递一个位置和一个 visitCount 来创建一个新的 LocationViewModel,但为此我首先获取所有位置,然后对于每个位置,我都试图获取一个 visitCount

addDisposable(mLocationProvider.getLocation()
            .flatMapSingle(locations -> Flowable.fromIterable(locations)
                    .flatMapSingle(singleLocation -> mVisitCountRepo.requestVisitCount(singleLocation.getId()))
                    .toList())
            .subscribe(visitCountList -> {


            }, Throwable::printStackTrace));

问题是我不知道如何将两者结合起来,所以我可以有类似的东西:

flatMapSinge(singleLocation,visitCount -> new LocationViewModel(singleLocation,visitCount).toList()

目前我在最后得到列表,任何帮助将不胜感激。

标签: android

解决方案


LocationViewModel你可以像这样在最后得到一个列表

  addDisposable(mLocationProvider.getLocation().flatMap(Flowable::fromIterable)
            .flatMap(singleLocation -> mVisitCountRepo.requestVisitCount(singleLocation.getId()).toFlowable()
                    .map(visitCount -> new LocationViewModel(singleLocation, visitCount)))
            .toList()
            .subscribeWith(new DisposableSingleObserver<List<LocationViewModel>>() {
                @Override
                public void onSuccess(List<LocationViewModel> locationViewModels) {

                }

                @Override
                public void onError(Throwable e) {

                }
            }));

推荐阅读