我有一个返回 Observable<ArrayList<Long>>
的方法,这些是一些项的id。我想通过另一个返回Observable<Item>
的方法遍历此列表并下载每个项。
如何使用RxJava操作符来完成这个任务?
我有一个返回 Observable<ArrayList<Long>>
的方法,这些是一些项的id。我想通过另一个返回Observable<Item>
的方法遍历此列表并下载每个项。
如何使用RxJava操作符来完成这个任务?
这是一个简短的自包含示例
public class Example {
public static class Item {
int id;
}
public static void main(String[] args) {
getIds()
.flatMapIterable(ids -> ids) // Converts your list of ids into an Observable which emits every item in the list
.flatMap(Example::getItemObservable) // Calls the method which returns a new Observable<Item>
.subscribe(item -> System.out.println("item: " + item.id));
}
// Simple representation of getting your ids.
// Replace the content of this method with yours
private static Observable<List<Integer>> getIds() {
return Observable.just(Arrays.<Integer>asList(1, 2, 3));
}
// Replace the content of this method with yours
private static Observable<Item> getItemObservable(Integer id) {
Item item = new Item();
item.id = id;
return Observable.just(item);
}
}
请注意,Observable.just(Arrays.<Integer>asList(1, 2, 3))
是您问题中Observable<ArrayList<Long>>
的简单表示。您可以在代码中将其替换为自己的Observable。这应该是您所需的基础。flatMapIterable
方法,因为它属于Iterable
,如下所述:/**
* Implementing this interface allows an object to be the target of
* the "for-each loop" statement. See
* <strong>
* <a href="{@docRoot}openjdk-redirect.html?v=8&path=/technotes/guides /language/foreach.html">For-each Loop</a>
* </strong>
*
* @param <T> the type of elements returned by the iterator
*
* @since 1.5
* @jls 14.14.2 The enhanced for statement
*/
public interface Iterable<T>
使用 flatMap
作为 flatMapIterable
的替代方案:
Observable.just(Arrays.asList(1, 2, 3)) //we create an Observable that emits a single array
.flatMap(numberList -> Observable.fromIterable(numberList)) //map the list to an Observable that emits every item as an observable
.flatMap(number -> downloadFoo(number)) //download smth on every number in the array
.subscribe(...);
private ObservableSource<? extends Integer> downloadFoo(Integer number) {
//TODO
}
个人认为.flatMap(numberList -> Observable.fromIterable(numberList))
比.flatMapIterable(numberList -> numberList)
更易读和理解。
这两者之间的区别在于顺序(RxJava2):
使用方法引用的示例如下:
Observable.just(Arrays.asList(1, 2, 3))
.flatMap(Observable::fromIterable)
.flatMap(this::downloadFoo)
flatMap
函数。您可以将其视为一个两步骤过程:
Iterable<T>
),并将其重新发出为一个Observable<T>
flatMap
将这些发出的Observable<T>
对象合并成单个Observable<T>
public class FlattenTransform<T> implements Observable.Transformer<Iterable<T>, T> {
@Override
public Observable<? extends T> call(Observable<? extends Iterable<T>> source) {
return source.flatMap(new Func1<Iterable<T>, Observable<T>>() {
@Override
public Observable<T> call(Iterable<T> values) {
return Observable.from(values);
}
});
}
}
compose
将转换应用于源observable:public class Example {
private static final ArrayList<Long> sourceList = new ArrayList<>(Arrays.asList(new Long[] {1L,2L,3L}));
private static final Observable<ArrayList<Long>> listObservable = Observable.just(sourceList);
private static final FlattenTransform<Long> flattenList = new FlattenTransform<Long>();
public static void main(String[] args) {
listObservable.compose(flattenList).subscribe(printItem);
}
private static Action1<Long> printItem = new Action1<Long>() {
@Override
public void call(Long item) {
System.out.println("item: " + item);
}
};
}
Transformer
的 compose
而不是 Func1
的 flatMap
的优点在于,如果将来需要再次展开列表,您甚至不必考虑使用哪个操作符(map?flatMap?concatMap?)。换句话说,flatmap 操作已经嵌入到 FlattenTransform 类中,这个细节被抽象化了。
Transformer 还有其他好处,例如能够链接多个操作。
flattenAsFlowable
:repository.getFlowableData(id)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.toList()
.flattenAsFlowable { it }
.map { someMethod(it) }
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ },
{ onError(it) })
针对Kotlin语言
Observable
.fromIterable(listOfChallenges) // list of challenges which contain challenge ID
.flatMap { eachChallenge ->
getChallengeDetail(eachChallenge.challengeUid!!) // fetch details of each challenge
}
.toList() // map to list
.subscribe({ listOfEachChallengesDetail ->
}, {
// error
})