RxSwift 小记-Working with Subjects

2018-05-24  本文已影响8人  sdupidBoby

(*useful)标记:目前觉得有用的函数
//FIXME 标记:待补充

Subject:Subject 可以看做是一种代理和桥梁

既可订阅消息,又可以reemitting ,emit new items

extension ObservableType {     //封装来方便调用调试的方法
    /**
     Add observer with `id` and print each emitted event.
     - parameter id: an identifier for the subscription.
     */
    func addObserver(_ id: String) -> Disposable {
        return subscribe { print("Subscription:", id, "Event:", $0) }
    }
}

func writeSequenceToConsole<O: ObservableType>(name: String, sequence: O) -> Disposable {
    return sequence.subscribe { event in
        print("Subscription: \(name), event: \(event)")
    }
}

PublishSubject:向所有订阅发广播

:订阅之后才能收到消息
    let disposeBag = DisposeBag()
    let subject = PublishSubject<String>()
    
    subject.addObserver("1").disposed(by: disposeBag)
    subject.onNext("🐶")
    subject.onNext("🐱")
    
    subject.addObserver("2").disposed(by: disposeBag)
    subject.onNext("🅰️")
    subject.onNext("🅱️")

打印:
Subscription: 1 Event: next(🐶)
Subscription: 1 Event: next(🐱)
Subscription: 1 Event: next(🅰️)
Subscription: 2 Event: next(🅰️) // 订阅了才能收到消息,前面的没有收到
Subscription: 1 Event: next(🅱️)
Subscription: 2 Event: next(🅱️)

ReplaySubject:向所有订阅发广播

并向新订阅发送先前事件(bufferSize决定往期个数,类似RACReplaySubject的热信号)。
  let disposeBag = DisposeBag()
    let subject = ReplaySubject<String>.create(bufferSize: 10)
    subject.addObserver("1").disposed(by: disposeBag)
    subject.onNext("🐶")
    subject.onNext("🐱")
    
    subject.addObserver("2").disposed(by: disposeBag)
    subject.onNext("🅰️")
    subject.onNext("🅱️")

打印:
Subscription: 1 Event: next(🐶)
Subscription: 1 Event: next(🐱)
Subscription: 2 Event: next(🐶) //这个就是往期发送的消息,
Subscription: 2 Event: next(🐱) //这里bufferSize为10,但emit只有两次
Subscription: 1 Event: next(🅰️)
Subscription: 2 Event: next(🅰️)
Subscription: 1 Event: next(🅱️)
Subscription: 2 Event: next(🅱️)

BehaviorSubject向所有订阅发广播

并向新订阅发送最近的一次emit消息,类似ReplaySubject的bufferSize为1
    let disposeBag = DisposeBag()
    let subject = BehaviorSubject(value: "🔴")
    
    subject.addObserver("1").disposed(by: disposeBag)
    subject.onNext("🐶")
    subject.onNext("🐱")
    
    subject.addObserver("2").disposed(by: disposeBag)
    subject.onNext("`🅰️`")
    subject.onNext("🅱️")
    
    subject.addObserver("3").disposed(by: disposeBag)
    subject.onNext("🍐")
    subject.onNext("🍊")

打印:
Subscription: 1 Event: next(🔴) //最近的一次emit消息
Subscription: 1 Event: next(🐶)
Subscription: 1 Event: next(🐱)
Subscription: 2 Event: next(🐱) //最近的一次emit消息
Subscription: 1 Event: next(🅰️)
Subscription: 2 Event: next(🅰️)
Subscription: 1 Event: next(🅱️)
Subscription: 2 Event: next(🅱️)
Subscription: 3 Event: next(🅱️) //最近的一次emit消息
Subscription: 1 Event: next(🍐)
Subscription: 2 Event: next(🍐)
Subscription: 3 Event: next(🍐)
Subscription: 1 Event: next(🍊)
Subscription: 2 Event: next(🍊)
Subscription: 3 Event: next(🍊)

Variable:

是基于 BehaviorSubject 的一层封装,它的优势是:不会被显式终结。即:不会emit .Error 事件,它会主动在析构的时候发送 .Complete 。
上一篇 下一篇

猜你喜欢

热点阅读