我应该什么时候使用 blockingGet?
When should I use blockingGet?
我在工作中经常使用 RxJava,并且看到了一些调用 returns Observable 或 Single 方法然后对其调用 blockingGet 以在不同的 .我在想这可能是对图书馆和概念的滥用,但我可能是错的。我举个小例子:
public Observable<String> getStrings(){
// return sg
}
public Observable<String> getNames(){
// return names
}
public Observable<String> filterNamesInStrings() {
List<String> names = getNames().toList().blockingGet();
return getStrings().filter(str -> /*is str in names list*/)
}
filterNamesInStrings
也可以通过以下方式解决:
getNames()
.toList()
.flatMapObservable(names->
getStrings().filter(str -> /*is str in names list*/)
我的直觉是第二种解决方案更好,但我唯一的原因是我觉得使用 blockingGet 我们有点打破了 observables 链,失去了懒惰(我不确定 Rx 有多懒惰是)但我没有找到任何东西来证明我的观点也没有什么可以进一步解释第二个更好。另外,如果我是对的,除了快速测试之外,我没有看到任何其他阻塞 get 的用例,是这样吗?
我的问题:
- 我的问题是有效的还是实现之间的差异可以忽略不计?
- 是否有任何解决方案 better/more 比其他解决方案更适合库,如果是这样,为什么以及是否有使用 blockingGet 的正当理由?
- (可选:你能给我推荐一本深入理解 ReactiveX 的好书吗,这样我就能得到对此类问题的解释,而且 "good practices" list/book 会很方便)
Is my question a valid one or the difference is negligible between the implementations?
blockingGet
阻塞当前线程,因此您几乎肯定不想在测试之外调用它。第二个例子是正确的做法。
Is any of the solutions better/more true to the library than the other, if so why and is there a valid reason to use blockingGet then?
主要是测试。或者很少,当使用完全同步的代码时,阻塞实际上不会发生。
understanding the depths of ReactiveX
没有这种深度的书。所有关于 RxJava 的书都很好读。
我在工作中经常使用 RxJava,并且看到了一些调用 returns Observable 或 Single 方法然后对其调用 blockingGet 以在不同的 .我在想这可能是对图书馆和概念的滥用,但我可能是错的。我举个小例子:
public Observable<String> getStrings(){
// return sg
}
public Observable<String> getNames(){
// return names
}
public Observable<String> filterNamesInStrings() {
List<String> names = getNames().toList().blockingGet();
return getStrings().filter(str -> /*is str in names list*/)
}
filterNamesInStrings
也可以通过以下方式解决:
getNames()
.toList()
.flatMapObservable(names->
getStrings().filter(str -> /*is str in names list*/)
我的直觉是第二种解决方案更好,但我唯一的原因是我觉得使用 blockingGet 我们有点打破了 observables 链,失去了懒惰(我不确定 Rx 有多懒惰是)但我没有找到任何东西来证明我的观点也没有什么可以进一步解释第二个更好。另外,如果我是对的,除了快速测试之外,我没有看到任何其他阻塞 get 的用例,是这样吗?
我的问题:
- 我的问题是有效的还是实现之间的差异可以忽略不计?
- 是否有任何解决方案 better/more 比其他解决方案更适合库,如果是这样,为什么以及是否有使用 blockingGet 的正当理由?
- (可选:你能给我推荐一本深入理解 ReactiveX 的好书吗,这样我就能得到对此类问题的解释,而且 "good practices" list/book 会很方便)
Is my question a valid one or the difference is negligible between the implementations?
blockingGet
阻塞当前线程,因此您几乎肯定不想在测试之外调用它。第二个例子是正确的做法。
Is any of the solutions better/more true to the library than the other, if so why and is there a valid reason to use blockingGet then?
主要是测试。或者很少,当使用完全同步的代码时,阻塞实际上不会发生。
understanding the depths of ReactiveX
没有这种深度的书。所有关于 RxJava 的书都很好读。