Android 如何使用RxJava返回值? 让我们考虑一下这种情况。我们有一个类,它有一个返回某些值的方法: public class Foo() { Observer<File> fileObserver; Observable<File> fileObservable; Subscription subscription; public File getMeThatThing(String id) { // implement logic in Observable<File> and return value which was // emitted in onNext(File) } } 公共类Foo(){ 观察员;观察员; 可观察的;可观察的; 认购; 公共文件GetMethatting(字符串id){ //在可观察值和返回值中实现逻辑 //在onNext(文件)中发出 } }

Android 如何使用RxJava返回值? 让我们考虑一下这种情况。我们有一个类,它有一个返回某些值的方法: public class Foo() { Observer<File> fileObserver; Observable<File> fileObservable; Subscription subscription; public File getMeThatThing(String id) { // implement logic in Observable<File> and return value which was // emitted in onNext(File) } } 公共类Foo(){ 观察员;观察员; 可观察的;可观察的; 认购; 公共文件GetMethatting(字符串id){ //在可观察值和返回值中实现逻辑 //在onNext(文件)中发出 } },android,rx-java,Android,Rx Java,如何返回在onNext中接收到的值?正确的方法是什么?谢谢。您首先需要更好地理解RxJava,什么是可观察->推送模型。这是供参考的解决方案: public class Foo { public static Observable<File> getMeThatThing(final String id) { return Observable.defer(() => { try { return Observ

如何返回在
onNext
中接收到的值?正确的方法是什么?谢谢。

您首先需要更好地理解RxJava,什么是可观察->推送模型。这是供参考的解决方案:

public class Foo {
    public static Observable<File> getMeThatThing(final String id) {
        return Observable.defer(() => {
          try {
            return Observable.just(getFile(id));
          } catch (WhateverException e) {
            return Observable.error(e);
          }
        });
    }
}


//somewhere in the app
public void doingThings(){
  ...
  // Synchronous
  Foo.getMeThatThing(5)
   .subscribe(new OnSubscribed<File>(){
     public void onNext(File file){ // your file }
     public void onComplete(){  }
     public void onError(Throwable t){ // error cases }
  });

  // Asynchronous, each observable subscription does the whole operation from scratch
  Foo.getMeThatThing("5")
   .subscribeOn(Schedulers.newThread())
   .subscribe(new OnSubscribed<File>(){
     public void onNext(File file){ // your file }
     public void onComplete(){  }
     public void onError(Throwable t){ // error cases }
  });

  // Synchronous and Blocking, will run the operation on another thread while the current one is stopped waiting.
  // WARNING, DANGER, NEVER DO IN MAIN/UI THREAD OR YOU MAY FREEZE YOUR APP
  File file = 
  Foo.getMeThatThing("5")
   .subscribeOn(Schedulers.newThread())
   .toBlocking().first();
  ....
}
公共类Foo{
公共静态可观察getMeThatThing(最终字符串id){
返回可观察的延迟(()=>{
试一试{
返回Observable.just(getFile(id));
}捕获(任何例外){
返回可观测误差(e);
}
});
}
}
//应用程序中的某个地方
公共事务{
...
//同步的
Foo.getMeThatThing(5)
.订阅(新的已订阅(){
public void onNext(文件){//your File}
公共void onComplete(){}
公共无效onError(可丢弃的t){//错误案例}
});
//异步,每个可观察订阅从头开始执行整个操作
Foo.getMeThatThing(“5”)
.subscribeOn(Schedulers.newThread())
.订阅(新的已订阅(){
public void onNext(文件){//your File}
公共void onComplete(){}
公共无效onError(可丢弃的t){//错误案例}
});
//同步和阻塞,将在当前线程停止等待时在另一个线程上运行该操作。
//警告,危险,切勿在主/用户界面线程中使用,否则您可能会冻结您的应用程序
文件=
Foo.getMeThatThing(“5”)
.subscribeOn(Schedulers.newThread())
.toBlocking().first();
....
}

谢谢。我刚开始用RxJava做实验。我的方法需要返回
文件
,而不是
可观察的
,因为应用程序的许多其他部分正在请求
文件
。那可能吗?RxJava的全部要点是,将pull模型(现在给我这个)转换为push模型(我在这里等待这个数据)。这需要重新思考你的方法。这样做的好处是推送模型是可组合的,单个错误处理,并行化操作非常简单。现在,除非您开始使用调度程序进行线程化和并行化,否则一旦subscribe命中,您的观察对象将同步执行操作。谢谢。我需要返回文件并且不可观察,因为我的类实现了某些我不能更改的接口。我想RxJava不适合我的情况。有一种方法。RxJava有一个名为.toBlocking()的方法,该方法将其转换为BlockingObservable。BO有一个名为.first()的方法,它同步返回接收到的第一个元素。在这种情况下,以这种方式使用RxJava并不会带来任何好处。我将把它添加到示例中。