java RxJava:如何从可观察对象中提取对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38744522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RxJava: How to extract object from observable?
提问by Daniil Orekhov
I feel like this is a dumb question, but I couldn't find any answer for a while, so I'm gonna ask it, sorry :)
我觉得这是一个愚蠢的问题,但我暂时找不到任何答案,所以我会问它,抱歉:)
So, I need a function that does the following:
所以,我需要一个执行以下操作的函数:
1) Calls another function to create an Observable User
1) 调用另一个函数来创建一个 Observable User
2) Gets the User object from the Observable User
2)从Observable User获取User对象
3) Gets some info about the user and runs through some logic
3) 获取一些关于用户的信息并运行一些逻辑
4) Returns Observable User
4) 返回可观察用户
I am having troubles with step #2. How do I do that? Or, is this approach somehow fundamentally wrong?
我在第 2 步时遇到了麻烦。我怎么做?或者,这种方法在某种程度上从根本上是错误的吗?
Here's the "model" of the function:
这是函数的“模型”:
@Override protected Observable buildUseCaseObservable(){
Observable<User> userObservable = userRepository.findUserByUsername(username);
//User user = ??????
//if (...) {...}
return userObservable;
}
Thank you :)
谢谢 :)
采纳答案by Rafay Ali
You cannot 'extract' something from an observable. You get items from observable when you subscribe to them (if they emit any). Since the object you are returning is of type Observable, you can apply operators to transform your data to your linking. The most common and easy to use operator in RxJava is 'map' which changes one form of data to other by applying a function.
您无法从可观察对象中“提取”某些内容。当您订阅它们时,您会从 observable 中获取项目(如果它们发出任何内容)。由于您返回的对象是 Observable 类型,您可以应用运算符将数据转换为链接。RxJava 中最常见且易于使用的运算符是“map”,它通过应用函数将一种形式的数据更改为另一种形式。
In your case, you can use 'map' operator directly on Observable<user>
:
在您的情况下,您可以直接在 'map' 运算符上使用Observable<user>
:
return userRepository.findUserByUsername(username)
.map(new Func1<User, Object>() {
@Override
public Object call(User u) {
// ..
// apply your logic here
// ..
return myDataObject; // return you data here to subcribers
}
});
I hope you know the basics of RxJava and doesn't need any introduction about how to use operators. For map documentation, follow this link
我希望你了解 RxJava 的基础知识,不需要任何关于如何使用运算符的介绍。有关地图文档,请点击此链接
回答by paul
You can use operators(map, flatMap, doOnNext, etc) to get the object wrapped by your observable through the pipeline
您可以使用运算符(map、flatMap、doOnNext 等)通过管道获取由您的 observable 包装的对象
Observable.just("hello world")
.map(sentence-> sentence.toUpperCase) --> do whatever you need.
.subscribe(sentence -> println(sentence)
By design Observable follow the Observer patter, which subscribe to the observable and receive the item once has been emitted through the pipeline.
通过设计 Observable 遵循 Observer 模式,它订阅 observable 并接收通过管道发出的项目。
Also what you can do is instead use observer patter, just extract the object from the pipeline using toBlocking. But that′s is consider an anti pattern and means you′re not applying a good design.
另外你可以做的是使用观察者模式,只需使用toBlocking从管道中提取对象。但那是一种反模式,意味着你没有应用一个好的设计。
@Test
public void observableEvolveAndReturnToStringValue() {
assertTrue(Observable.just(10)
.map(String::valueOf)
.toBlocking()
.single()
.equals("10"));
}
You can see more examples about to Blocking here https://github.com/politrons/reactive/blob/master/src/test/java/rx/observables/utils/ObservableToBlocking.java
您可以在此处查看有关阻止的更多示例https://github.com/politrons/reactive/blob/master/src/test/java/rx/observables/utils/ObservableToBlocking.java