I have a method that uploads concurrently multiple files to the cloud storage. It looks something like this:
List<String> files = Arrays.asList("file0", "file1", "file2");
Observable.from(files)
.flatMap(file -> uploadFile(file)
.flatMap(done -> notifyFinished(file)))
.subscribe(this::onNext, this::onError, this::onCompleted);
private Observable<Boolean> uploadFile(String file) {
Timber.d("Uploading: " + file);
return Observable.just(true).delay(6, TimeUnit.SECONDS);
}
private Observable<Boolean> notifyFinished(String file) {
Timber.d("Notify finished: " + file);
return Observable.just(true).delay(3, TimeUnit.SECONDS);
}
The output of this is:
06-09 02:10:04.779 D: Uploading: file0
06-09 02:10:04.780 D: Uploading: file1
06-09 02:10:04.781 D: Uploading: file2
06-09 02:10:10.782 D: Notify finished: file1
06-09 02:10:10.782 D: Notify finished: file0
06-09 02:10:10.783 D: Notify finished: file2
06-09 02:10:13.784 D: onNext
06-09 02:10:13.786 D: onNext
06-09 02:10:13.786 D: onNext
06-09 02:10:13.787 D: onCompleted
I want to make it work sequentially, eg:
1) Uploading: file0
2) Notify finished: file0
3) onNext
4) Uploading: file1
5) Notify finished: file1
6) onNext
...
Is it possible to do something like this with Rx?
EDIT
Replacing first flatMap
with concatMap
did the job. I thought that I knew the difference between those operators, but this example just shown that I know nothing... Now the output is:
06-09 02:15:00.581 D: Uploading: file0
06-09 02:15:06.584 D: Notify finished: file0
06-09 02:15:09.586 D: onNext
06-09 02:15:09.587 D: Uploading: file1
06-09 02:15:15.590 D: Notify finished: file1
06-09 02:15:18.593 D: onNext
06-09 02:15:18.595 D: Uploading: file2
06-09 02:15:24.598 D: Notify finished: file2
06-09 02:15:27.599 D: onNext
06-09 02:15:27.601 D: onCompleted
如果你想要'有序'的连续顺序,只需使用concatMap()
而不是flatMap()
Create an observable per file and concat the three observables
@Test
public void testContact() {
Observable.concat(Observable.just(uploadFile(file1)),
Observable.just(uploadFile(file2)),
Observable.just(uploadFile(file3)))
.flatMap(file -> notifyFinished(file)))
.subscribe(this::onNext, this::onError, this::onCompleted);
}
You will have to make the method notifyFinished return the observable file instead boolean.
You can also use merge or zip, you have more examples of combining observables here https://github.com/politrons/reactive/tree/master/src/test/java/rx/observables/combining
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.