拼合观测&LT;观测&LT;光标&GT;&GT;可观察&LT;光标&GT; [英] Flatten Observable<Observable<Cursor>> to Observable<Cursor>
问题描述
我有一个观测
返回单个光标
实例(观测&LT;光标&GT ;
)。我想利用 ContentObservable.fromCursor
来获得 onNext
回调每个光标所在行。
一,我想通了,解决方法是这样的结构:
ContentObservable.fromCursor(cursorObservable.toBlocking()。第一个())
.subscribe(光标 - &GT; {
//地图对象
//添加到外集合
},E - &GT; {},() - &GT; {
//做一些与对象列表(外集)
});
这看起来有点像,因为 toBlocking()黑客攻击。第一个()
,但它的作品。我不喜欢它,因为大部分的处理在 onNext
完成回调,我们已经创造外收集来保存中间结果。
我想用这样的:
cursorObservable.map(ContentObservable :: fromCursor)
.MAP(fromCursor - &GT; fromCursor.toBlocking()第())
.MAP(/ *映射为对象* /)
.toList()
.subscribe(对象 - &GT; {
//做一些与对象列表
}
这还是利用 toBlocking()。第一个()
并不起作用,因为一旦 fromCursor
观察到有完成光标被关闭,所以没有办法映射到对象。有没有更好的方式来压平观测&LT;观测&LT;光标&GT;&GT;
到观测&LT;光标&GT;
有没有更好的方式来压平
观测&LT;观测&LT;光标&GT;&GT;
到观测&LT;光标&GT;
?
是的,你可以使用<一个href="http://reactivex.io/RxJava/javadoc/rx/Observable.html#concat(rx.Observable)"><$c$c>Observable.concat$c$c>方法:
公共静态无效的主要(字串[] args){
最后的观测和LT;字符串&GT;可观察= Observable.just(1,2);
最后的观测和LT;观测&LT;字符串&GT;&GT;嵌套= observable.map(价值 - &GT; Observable.just(价值+1,值+2));
最后的观测和LT;字符串&GT;扁平= Observable.concat(嵌套);
flattened.subscribe(的System.out ::的println);
}
更新
实际上有一些其他的方法来转换观测&LT;观测&LT;光标&GT;&GT;
到观测&LT;光标&GT;
:
- <一个href="http://reactivex.io/RxJava/javadoc/rx/Observable.html#concat(rx.Observable)">Observable.concat
- <一个href="http://reactivex.io/RxJava/javadoc/rx/Observable.html#merge(rx.Observable)">Observable.merge
- <一个href="http://reactivex.io/RxJava/javadoc/rx/Observable.html#switchOnNext(rx.Observable)">Observable.switchOnNext
只需选择一个更适合于你。
更新2
另一种解决方案是修改codeA位,并使用 flatMap
运营商,而不是地图
:
cursorObservable.flatMap(ContentObservable :: fromCursor)
.MAP(/ *映射为对象* /)
.toList()
.subscribe(对象 - &GT; {
//做一些与对象列表(外集)
}
I've an Observable
that returns a single Cursor
instance (Observable<Cursor>
). I'm trying to utilize ContentObservable.fromCursor
to obtain every cursor's row in onNext
callback.
One of the solutions I've figured out is such construction:
ContentObservable.fromCursor(cursorObservable.toBlocking().first())
.subscribe(cursor -> {
// map to object
// add to outer collection
}, e -> {}, () -> {
// do something with list of objects (outer collection)
});
This looks rather like a hack because of toBlocking().first()
, but it works. I don't like it because most of the processing is done in onNext
callback and we've to create outer collection to hold the intermediate results.
I wanted to use it like this:
cursorObservable.map(ContentObservable::fromCursor)
.map(fromCursor -> fromCursor.toBlocking().first())
.map(/* map to object */)
.toList()
.subscribe(objects -> {
// do something with list of objects
}
This still utilizes toBlocking().first()
and doesn't work because once the fromCursor
observable has finished the cursor is closed so there's no way to map it to object. Is there a better way to flatten Observable<Observable<Cursor>>
to Observable<Cursor>
?
Is there a better way to flatten
Observable<Observable<Cursor>>
toObservable<Cursor>
?
Yes, you can use Observable.concat
method:
public static void main(String[] args) {
final Observable<String> observable = Observable.just("1", "2");
final Observable<Observable<String>> nested = observable.map(value -> Observable.just(value + "1", value + "2"));
final Observable<String> flattened = Observable.concat(nested);
flattened.subscribe(System.out::println);
}
UPDATE
There are actually some other methods to transform an Observable<Observable<Cursor>>
to Observable<Cursor>
:
Just choose one that is more appropriate to you.
UPDATE 2
Another solution is to modify your code a bit and use a flatMap
operator instead of map
:
cursorObservable.flatMap(ContentObservable::fromCursor)
.map(/* map to object */)
.toList()
.subscribe(objects -> {
// do something with list of objects (outer collection)
}
这篇关于拼合观测&LT;观测&LT;光标&GT;&GT;可观察&LT;光标&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!