在RxJava中使用FlatMap 2(Using FlatMap in RxJava 2)

编程入门 行业动态 更新时间:2024-10-28 12:21:33
RxJava中使用FlatMap 2(Using FlatMap in RxJava 2)

我在一个新项目上使用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); } });

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

I'm using RxJava 2 on a new project (I've been using RxJava 1 for a long time) and I have some problems with using a flatMap (or maybe flatMapSingle?). There seems to be something I'm missing in the whole concept.

mObjectManager.getAllObjects returns a AsyncProcessor<List<Object>>. (I replaced the actual Class name with 'Object').

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); } });

I'm wondering how I can transform the list to a SingleSource? How is the flatMap being used in RxJava 2?

最满意答案

好吧,我终于找到了答案。 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); } })

Okay, I found the answer after all. Flowable.fromIterable does the trick!

... .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); } })

更多推荐

本文发布于:2023-07-07 04:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1058927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:RxJava   FlatMap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!