我有一个Observable返回单个Cursor实例(Observable<Cursor>).我正在尝试利用回调中ContentObservable.fromCursor获取每个光标的行onNext.
我发现的解决方案之一是这样的结构:
ContentObservable.fromCursor(cursorObservable.toBlocking().first())
.subscribe(cursor -> {
// map to object
// add to outer collection
}, e -> {}, () -> {
// do something with list of objects (outer collection)
});
Run Code Online (Sandbox Code Playgroud)
这听起来像是一个黑客toBlocking().first(),但它有效.我不喜欢它,因为大部分处理是在onNext回调中完成的,我们要创建外部集合来保存中间结果.
我想这样使用它:
cursorObservable.map(ContentObservable::fromCursor)
.map(fromCursor -> fromCursor.toBlocking().first())
.map(/* map to object */)
.toList()
.subscribe(objects -> {
// do something with list of objects
}
Run Code Online (Sandbox Code Playgroud)
这仍然有用toBlocking().first()并且不起作用,因为一旦fromCursorobservable完成,光标就会关闭,所以无法将它映射到对象.有没有更好的方式来拉平Observable<Observable<Cursor>>来Observable<Cursor>?