学而不思则罔,思而不学则殆
【RxJava】RxJava系列文章之just+fromArray操作符
简单介绍
将数组变为可观察对象,并发射出去
简单使用
Demo1
private static void testJust() {
//1.创建observable
@NonNull Observable<Object> observable = Observable.just(1, 2, 3, 4);
System.out.println("observable:"+observable);
//2.创建观察者
Observer<Object> observer = new Observer<Object>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
System.out.println("onSubscribe:" + d.getClass());
}
@Override
public void onNext(@NonNull Object o) {
System.out.println("onNext:" + o);
}
@Override
public void onError(@NonNull Throwable e) {
System.out.println("onError");
}
@Override
public void onComplete() {
System.out.println("onComplete");
}
};
//3.建立订阅关系
observable.subscribe(observer);
}
observable:io.reactivex.rxjava3.internal.operators.observable.ObservableFromArray@42a57993
onSubscribe:class io.reactivex.rxjava3.internal.operators.observable.ObservableFromArray$FromArrayDisposable
onNext:1
onNext:2
onNext:3
onNext:4
onComplete
Demo2
private static void testJust() {
//1.创建observable
@NonNull Observable<Object> observable = Observable.just(5);
System.out.println("observable:"+observable);
//2.创建观察者
Observer<Object> observer = new Observer<Object>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
System.out.println("onSubscribe:" + d.getClass());
}
@Override
public void onNext(@NonNull Object o) {
System.out.println("onNext:" + o);
}
@Override
public void onError(@NonNull Throwable e) {
System.out.println("onError");
}
@Override
public void onComplete() {
System.out.println("onComplete");
}
};
//3.建立订阅关系
observable.subscribe(observer);
}
observable:io.reactivex.rxjava3.internal.operators.observable.ObservableJust@42a57993
onSubscribe:class io.reactivex.rxjava3.internal.operators.observable.ObservableScalarXMap$ScalarDisposable
onNext:5
onComplete
Demo3
private static void testJust() {
//1.创建observable
@NonNull Observable<Object> observable = Observable.fromArray();
System.out.println("observable:" + observable);
//2.创建观察者
Observer<Object> observer = new Observer<Object>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
System.out.println("onSubscribe:" + d.getClass());
}
@Override
public void onNext(@NonNull Object o) {
System.out.println("onNext:" + o);
}
@Override
public void onError(@NonNull Throwable e) {
System.out.println("onError");
}
@Override
public void onComplete() {
System.out.println("onComplete");
}
};
//3.建立订阅关系
observable.subscribe(observer);
observable:io.reactivex.rxjava3.internal.operators.observable.ObservableEmpty@42a57993
onSubscribe:class io.reactivex.rxjava3.internal.disposables.EmptyDisposable
onComplete
原理分析
查看just源码,知道内部调用的是fromArray
public static <T> Observable<T> just(@NonNull T item1, @NonNull T item2, @NonNull T item3, @NonNull T item4) {
Objects.requireNonNull(item1, "item1 is null");
Objects.requireNonNull(item2, "item2 is null");
Objects.requireNonNull(item3, "item3 is null");
Objects.requireNonNull(item4, "item4 is null");
return fromArray(item1, item2, item3, item4);
}
再看fromArray源码
public static <T> Observable<T> fromArray(@NonNull T... items) {
Objects.requireNonNull(items, "items is null");
if (items.length == 0) {
return empty();
}
if (items.length == 1) {
return just(items[0]);
}
return RxJavaPlugins.onAssembly(new ObservableFromArray<>(items));
}
这里主要分为三种情况,0 1 和其他
public static <T> Observable<T> empty() {
return RxJavaPlugins.onAssembly((Observable<T>) ObservableEmpty.INSTANCE);
}
public static <T> Observable<T> just(@NonNull T item) {
Objects.requireNonNull(item, "item is null");
return RxJavaPlugins.onAssembly(new ObservableJust<>(item));
}
对应的Observable是:
参数长度 | Observable |
---|---|
0 | ObservableEmpty |
1 | ObservableJust |
>1 | ObservableFromArray |
ObservableEmpty
ObservableEmpty 源码分析
当观察个数为0的时候会创建ObservableEmpty
源码如下:
//ObservableEmpty.java
public final class ObservableEmpty extends Observable<Object> implements ScalarSupplier<Object> {
public static final Observable<Object> INSTANCE = new ObservableEmpty();
...
@Override
protected void subscribeActual(Observer<? super Object> o) {
EmptyDisposable.complete(o);
}
...
}
//EmptyDisposable.java
public enum EmptyDisposable implements QueueDisposable<Object> {
/**
* Since EmptyDisposable implements QueueDisposable and is empty,
* don't use it in tests and then signal onNext with it;
* use Disposables.empty() instead.
*/
INSTANCE,
/**
* An empty disposable that returns false for isDisposed.
*/
NEVER
;
...
public static void complete(Observer<?> observer) {
observer.onSubscribe(INSTANCE);
observer.onComplete();
}
...
}
当我们订阅的时候:
//3.建立订阅关系
observable.subscribe(observer);
会调用ObservableEmpty.subscribeActual方法,然后该方法调用EmptyDisposable.complete方法,内部只是简单地调用了Observer对象的onSubscribe和onComplete方法。
ObservableEmpty 官方的图
ObservableEmpty 时序图
一句话描述:拿到订阅者只调用了onSubscribe和onComplete方法。
ObservableJust
ObservableJust 源码解析
订阅的时候走到subscribeActual,该方法内部走到ScalarDisposable的run方法。
public final class ObservableJust<T> extends Observable<T> implements ScalarSupplier<T> {
private final T value;
public ObservableJust(final T value) {
this.value = value;
}
@Override
protected void subscribeActual(Observer<? super T> observer) {
ScalarDisposable<T> sd = new ScalarDisposable<>(observer, value);
observer.onSubscribe(sd);
sd.run();
}
...
}
//ObservableScalarXMap.java
public static final class ScalarDisposable<T> extends AtomicInteger implements QueueDisposable<T>, Runnable {
final Observer<? super T> observer;
final T value;
...
public ScalarDisposable(Observer<? super T> observer, T value) {
this.observer = observer;
this.value = value;
}
...
@Override
public void run() {
if (get() == START && compareAndSet(START, ON_NEXT)) {
observer.onNext(value); //调用订阅者的onNext方法
if (get() == ON_NEXT) {
lazySet(ON_COMPLETE);
observer.onComplete(); //调用订阅者的onComplete方法
}
}
}
}
ObservableJust 官方用图
ObservableJust 时序图
一句话总结:ScalarDisposable的run方法中调用订阅者的onNext方法进行分发。
ObservableFromArray
ObservableFromArray 源码解析
public final class ObservableFromArray<T> extends Observable<T> {
final T[] array;
public ObservableFromArray(T[] array) {
this.array = array;
}
@Override
public void subscribeActual(Observer<? super T> observer) {
FromArrayDisposable<T> d = new FromArrayDisposable<>(observer, array);
observer.onSubscribe(d);
if (d.fusionMode) {
return;
}
d.run();
}
static final class FromArrayDisposable<T> extends BasicQueueDisposable<T> {
final Observer<? super T> downstream;
final T[] array;
...
FromArrayDisposable(Observer<? super T> actual, T[] array) {
this.downstream = actual;
this.array = array;
}
...
void run() {
T[] a = array;
int n = a.length;
for (int i = 0; i < n && !isDisposed(); i++) {
T value = a[i];
if (value == null) {
downstream.onError(new NullPointerException("The element at index " + i + " is null"));
return;
}
downstream.onNext(value);
}
if (!isDisposed()) {
downstream.onComplete();
}
}
}
}
这个跟ScalarDisposable中的run方法很像,只是这里是数组的for循环
ObservableFromArray 官方用图
ObservableFromArray 时序图
一句话总结:FromArrayDisposable的run方法中循环调用订阅者的onNext方法进行分发