[关闭]
@TryLoveCatch 2021-04-09T10:28:39.000000Z 字数 1736 阅读 1358

Android第三方框架之Rxjava

android 第三方框架 Rxjava


Rxjava

BehaviorSubject

  1. // BehaviorSubject behaviorSubject = BehaviorSubject.create();// 没有默认值
  2. BehaviorSubject behaviorSubject = BehaviorSubject.create("-1");
  3. behaviorSubject.onNext("1");
  4. behaviorSubject.onNext("2");
  5. behaviorSubject.subscribe(new Observer() {
  6. @Override
  7. public void onCompleted() {
  8. LogUtil.log("behaviorSubject:complete");
  9. }
  10. @Override
  11. public void onError(Throwable e) {
  12. LogUtil.log("behaviorSubject:error");
  13. }
  14. @Override
  15. public void onNext(String s) {
  16. LogUtil.log("behaviorSubject:"+s);
  17. }
  18. });
  19. behaviorSubject.onNext("3");
  20. behaviorSubject.onNext("4");

拾遗

壹 CompositeSubscription

clear和unsubscribe

CompositeSubscription.unsubscribe()解绑后无法继续add使用

  1. public void clear() {
  2. if (!unsubscribed) {
  3. Collection<Subscription> unsubscribe;
  4. synchronized (this) {
  5. if (unsubscribed || subscriptions == null) {
  6. return;
  7. } else {
  8. unsubscribe = subscriptions;
  9. subscriptions = null;
  10. }
  11. }
  12. unsubscribeFromAll(unsubscribe);
  13. }
  14. }
  15. public void unsubscribe() {
  16. if (!unsubscribed) {
  17. Collection<Subscription> unsubscribe;
  18. synchronized (this) {
  19. if (unsubscribed) {
  20. return;
  21. }
  22. unsubscribed = true;
  23. unsubscribe = subscriptions;
  24. subscriptions = null;
  25. }
  26. // we will only get here once
  27. unsubscribeFromAll(unsubscribe);
  28. }
  29. }

这两个方法其实就一个地方的差别,就是在unsubscribe()中将unsubscribed = true,那么这个有什么用呢?

  1. public void add(final Subscription s) {
  2. if (s.isUnsubscribed()) {
  3. return;
  4. }
  5. if (!unsubscribed) {
  6. synchronized (this) {
  7. if (!unsubscribed) {
  8. if (subscriptions == null) {
  9. subscriptions = new HashSet<Subscription>(4);
  10. }
  11. subscriptions.add(s);
  12. return;
  13. }
  14. }
  15. }
  16. s.unsubscribe();
  17. }

由于unsubscribed = true,所以会执行s.unsubscribe();,直接解绑。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注