我在一个新项目上使用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); } })更多推荐
发布评论