Rx java RxJava中具有多个回调的单个操作

Rx java RxJava中具有多个回调的单个操作,rx-java,Rx Java,RxJava中应该如何公开具有多个回调的单个操作(例如,以某些参数开始,以其他参数多次调用进度,以其他参数结束) 我正在考虑使用一个包装器对象,它包含每种回调的一个可观察对象,并且其中任何一个可观察对象上的订阅都会触发操作的开始,而另一个绑定在相同的底层操作上 编辑:下载操作的回调接口示例 您可以将您的操作视为可观察的,其中 public static class Info { public final long contentLength; public final Stri

RxJava中应该如何公开具有多个回调的单个操作(例如,以某些参数开始,以其他参数多次调用进度,以其他参数结束)

我正在考虑使用一个包装器对象,它包含每种回调的一个可观察对象,并且其中任何一个可观察对象上的订阅都会触发操作的开始,而另一个绑定在相同的底层操作上

编辑:下载操作的回调接口示例


您可以将您的操作视为可观察的
,其中

public static class Info {
    public final long contentLength;
    public final String mimeType;
    public final String nameHint;
    public Info(long contentLength, String mimeType, String nameHint) {
        this.contentLength = contentLength;
        this.mimeType = mimeType;
        this.nameHint = nameHint;
    }
}
public static class Status {
    public final Info info;
    public final long downloadProgress; //in bytes
    public final Optional<File> file;
    public Status(Info info, long downloadProgress, Optional<File> file) {
        this.info = info;
        this.downloadProgress = downloadProgress;
        this.file = file;
    }
}

你想执行什么操作?@TassosBassoukos添加了一个回调接口示例,令我害怕的是对象分配/gc暂停(我在安卓系统上)事件,例如基于大量生成字节的进度。在这种情况下,在每个缓冲区读/写(例如8K)时,它将分配一个新的状态对象?是的,这是正确的,但您不必每8K发出一次。您拥有所有需要的计数信息,以确保您只发出1%或5%的信号,而这只是一个微不足道的gc量。
public static class Info {
    public final long contentLength;
    public final String mimeType;
    public final String nameHint;
    public Info(long contentLength, String mimeType, String nameHint) {
        this.contentLength = contentLength;
        this.mimeType = mimeType;
        this.nameHint = nameHint;
    }
}
public static class Status {
    public final Info info;
    public final long downloadProgress; //in bytes
    public final Optional<File> file;
    public Status(Info info, long downloadProgress, Optional<File> file) {
        this.info = info;
        this.downloadProgress = downloadProgress;
        this.file = file;
    }
}
download()
   .doOnNext(status -> System.out.println(
        "downloaded " 
        + status.downloadProgress 
        + " bytes of " + status.contentLength))
   .last()
   .doOnNext(status -> System.out.println(
        "downloaded " + status.file.get())
   .doOnError(e -> logError(e))
   .subscribe();