I am reading the file from a remote URL and using RxJava to report the progress of the download. The Observable file writer emits a sequence of DownloadProgress objects. Since many elements are emitted, I use Observable.sample () to control backpressure. This works very well - user interface updates arrive at a constant speed and there are no backpressure problems, but the latest progress update is almost always skipped.
I would like to get the last element in the Observable sequence so that I can update the interface with final progress. What is the best way to ensure that the last element in an Observable sequence is always emitted?
Observable<Response> fileReader =
Rx.okHttpGetRequest(url);
OkHttpResponseWriter fileWriter =
Rx.okHttpResponseWriter(outFile);
Subscription subscription = fileReader.flatMap(fileWriter)
.sample(1, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<DownloadProgress>() {
@Override
public void onCompleted() {}
@Override
public void onError(Throwable e) {}
@Override
public void onNext(DownloadProgress progress) {
}
});