Observable
發出的值Observable
subscribe()
後執行函數Observable
,進而執行這個Observable
裏的訂閱動做2
步聚中 Observable
輸出的值,收集完成後,發出這些值Observable
輸出的順序@Test public void flatMap2() throws Exception { final List<String> items = Lists.newArrayList("a", "b", "c", "d", "e", "f"); final TestScheduler scheduler = new TestScheduler(); Observable.fromIterable(items) .flatMap((Function<String, ObservableSource<String>>) s -> { System.out.println("x-" + s); final int delay = new Random().nextInt(15); return Observable.create((ObservableOnSubscribe<String>) emitter -> { System.out.println("y-" + s); emitter.onNext(s); emitter.onComplete(); }).delay(delay, TimeUnit.SECONDS, scheduler); // 這裏 delay 時長不一樣,致使最終數據順序變化 }) .subscribe(System.out::println); scheduler.advanceTimeBy(1, TimeUnit.MINUTES); }
x-a y-a x-b y-b x-c y-c x-d y-d x-e y-e x-f y-f f e b d a cjava