【问题标题】:RxJava Bigger Application Example / How do multiple Observable's fitRxJava 更大的应用程序示例 / 多个 Observable 如何适合
【发布时间】:2015-12-11 16:42:33
【问题描述】:

目前我从 observable 开始,但是目前我遇到了一些问题..

目前我有一个主类,这些类将调用 Observable 文件观察器,到目前为止一切都很好。这行得通。

类似这样的:

public static void main(String args[]) {
     Observable<String> myObservable = observable();
     // Buffer on Backpressure since I load every old file/directory, too
     Subscription subscription = observable.onBackpressureBuffer() 
        .subscribe(subscriber -> {

        })
    ... // Code that stop the program from stopping
}

所以这就是我所知道的,但我知道我想要一些其他 Observable 的东西,比如 OfficeObservablePdfObservable。当文件具有.pdf 的扩展名时将使用它,它将使用 PdfObservable。如果它有类似.docx 的东西,它应该调用OfficeObservable。但是我怎么能把它附加到一个更大的程序上。特别是因为这不会是我想使用的最后一个 observable。

我可以在 subscribe 方法或 flatMap 中将它们链接在一起,让它们都返回相同的接口吗? 我被困在如何从 RxJava 中获得更大的应用程序。

【问题讨论】:

  • 你能澄清你的问题吗?

标签: rx-java


【解决方案1】:

您可以像这样公开您的专用(和 FileObservable):

Observable<String> fileObservable;

private void init(){ // called from constructor or some other init'ish method
  //init your fileObservable
}    

public Observable<String> getFileObservable(){
  return fileObservable;  
}
public Observable<String> getPdfFileObservable(){
  return fileObservable.filter(name -> name.endsWith(".pdf"))
}

这很大程度上取决于你想做什么,如果你想在你的 fileObservable 中添加 cache() 或放入其他有用的 Observable 运算符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    相关资源
    最近更新 更多