在RxJava中使用FlatMap 2

YP_*_*_be 1 android rx-java2

我在一个新项目上使用RxJava 2(我已经使用了RxJava 1很长时间了)我在使用flatMap(或者flatMapSingle?)时遇到了一些问题.在整个概念中似乎都缺少一些东西.

mObjectManager.getAllObjects返回一个AsyncProcessor<List<Object>>.(我用'Object'替换了实际的Class名称).

Disposable subscription = mObjectManager.getAllObjects()
                .flatMapSingle(new Function<List<Object>, SingleSource<Object>>() {
                    @Override
                    public SingleSource<Object > apply(@io.reactivex.annotations.NonNull List<Object> objects) throws Exception {
                        // TODO WHAT GOES HERE?!
                    }
                }).filter(new Predicate<Object>() {
                    @Override
                    public boolean test(@io.reactivex.annotations.NonNull Object object) throws Exception {
                        return TextUtils.isEmpty(mSearchTerm) || object.name.toLowerCase().contains(mSearchTerm.toLowerCase());
                    }
                }).toSortedList(new Comparator<Object>() {
                    @Override
                    public int compare(Object c1, Object c2) {
                        return c1.name.compareTo(c2.name);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<Object>>() {
                    @Override
                    public void accept(@io.reactivex.annotations.NonNull List<Object> objects) throws Exception {
                        processObjects(objects);
                    }
                });
Run Code Online (Sandbox Code Playgroud)

我想知道如何将列表转换为SingleSource?如何在RxJava 2中使用flatMap?

YP_*_*_be 6

好的,毕竟我找到了答案. Flowable.fromIterable诀窍!

...
.flatMap(new Function<List<Object>, Publisher< Object >>() {
    @Override
    public Publisher< Object > apply(@io.reactivex.annotations.NonNull List< Object > objects) throws Exception {
        return Flowable.fromIterable(objects);
   }
})
Run Code Online (Sandbox Code Playgroud)