@TryLoveCatch
2021-04-09T10:28:39.000000Z
字数 1736
阅读 1358
android
第三方框架
Rxjava
// BehaviorSubject behaviorSubject = BehaviorSubject.create();// 没有默认值
BehaviorSubject behaviorSubject = BehaviorSubject.create("-1");
behaviorSubject.onNext("1");
behaviorSubject.onNext("2");
behaviorSubject.subscribe(new Observer() {
@Override
public void onCompleted() {
LogUtil.log("behaviorSubject:complete");
}
@Override
public void onError(Throwable e) {
LogUtil.log("behaviorSubject:error");
}
@Override
public void onNext(String s) {
LogUtil.log("behaviorSubject:"+s);
}
});
behaviorSubject.onNext("3");
behaviorSubject.onNext("4");
CompositeSubscription.unsubscribe()解绑后无法继续add使用
public void clear() {
if (!unsubscribed) {
Collection<Subscription> unsubscribe;
synchronized (this) {
if (unsubscribed || subscriptions == null) {
return;
} else {
unsubscribe = subscriptions;
subscriptions = null;
}
}
unsubscribeFromAll(unsubscribe);
}
}
public void unsubscribe() {
if (!unsubscribed) {
Collection<Subscription> unsubscribe;
synchronized (this) {
if (unsubscribed) {
return;
}
unsubscribed = true;
unsubscribe = subscriptions;
subscriptions = null;
}
// we will only get here once
unsubscribeFromAll(unsubscribe);
}
}
这两个方法其实就一个地方的差别,就是在unsubscribe()中将unsubscribed = true
,那么这个有什么用呢?
public void add(final Subscription s) {
if (s.isUnsubscribed()) {
return;
}
if (!unsubscribed) {
synchronized (this) {
if (!unsubscribed) {
if (subscriptions == null) {
subscriptions = new HashSet<Subscription>(4);
}
subscriptions.add(s);
return;
}
}
}
s.unsubscribe();
}
由于unsubscribed = true
,所以会执行s.unsubscribe();
,直接解绑。