Reproduced, please indicate the source as a link:
This article comes from: 103style's blog
Conversion-related operator and the official introduction
RxJava
The flatMap
series conversion operator official description: Transforming observables
flatMap
flatMapCompletable
flatMapIterable
flatMapMaybe
flatMapObservable
flatMapPublisher
flatMapSingle
flatMapSingleElement
Here we just a specific implementation, please refer to the intermediate flow RxJava create the source operator resolved .
flatMap
- Official Example:
Output:Observable.just("A", "B", "C") .flatMap(a -> { return Observable.intervalRange(1, 3, 0, 1, TimeUnit.SECONDS) .map(b -> '(' + a + ", " + b + ')'); }) .blockingSubscribe(System.out::println);
(A, 1) (C, 1) (B, 1) (A, 2) (C, 2) (B, 2) (A, 3) (C, 3) (B, 3)
- Returns the object
ObservableFlatMap
'ssubscribeActual
methods:public void subscribeActual(Observer<? super U> t) { if (ObservableScalarXMap.tryScalarXMapSubscribe(source, t, mapper)) { return; } source.subscribe(new MergeObserver<T, U>(t, mapper, delayErrors, maxConcurrency, bufferSize)); }
-
MergeObserver
TheonNext
method:public void onNext(T t) { // safeguard against misbehaving sources ... subscribeInner(p); } void subscribeInner(ObservableSource<? extends U> p) { for (;;) { if (p instanceof Callable) { ... drain(); ... } else { InnerObserver<T, U> inner = new InnerObserver<T, U>(this, uniqueId++); if (addInner(inner)) { p.subscribe(inner); } break; } } }
-
InnerObserver
TheonNext
method:public void onNext(U t) { if (fusionMode == QueueDisposable.NONE) { parent.tryEmit(t, this); } else { parent.drain(); } }
-
MergeObserver
Thedrain()
method:
Eventually call-entered thevoid drain() { if (getAndIncrement() == 0) { drainLoop(); } } void drainLoop() { final Observer<? super U> child = this.downstream;//1.0 int missed = 1; for (;;) { ... SimplePlainQueue<U> svq = queue; if (svq != null) { for (;;) { ... U o = svq.poll(); if (o == null) { break; } child.onNext(o);//1.1 } } ... if (d && (svq == null || svq.isEmpty()) && n == 0 && nSources == 0) { Throwable ex = errors.terminate(); if (ex != ExceptionHelper.TERMINATED) { if (ex == null) { child.onComplete();//1.2 } else { child.onError(ex);//1.3 } } return; } ... } }
observer
corresponding method.
Reproduced in: https: //www.jianshu.com/p/191a8680b8cc