在 RxJava 2 中使用 FlatMap
Using FlatMap in RxJava 2
我在一个新项目中使用 RxJava 2(我已经使用 RxJava 1 很长时间了),我在使用 flatMap(或者 flatMapSingle?)时遇到了一些问题。
我似乎在整个概念中遗漏了一些东西。
mObjectManager.getAllObjectsreturns一个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?
flatMap 在 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);
}
})
我在一个新项目中使用 RxJava 2(我已经使用 RxJava 1 很长时间了),我在使用 flatMap(或者 flatMapSingle?)时遇到了一些问题。 我似乎在整个概念中遗漏了一些东西。
mObjectManager.getAllObjectsreturns一个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? flatMap 在 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);
}
})