just share

2018 - 01 - 02

let signal = Observable<TimeInterval>
    .create { (observe) -> Disposable in
        print("---------------------------new")
        observe.onNext(Date().timeIntervalSince1970)
        DispatchQueue.after(.seconds(1)) {
            observe.onNext(Date().timeIntervalSince1970)
            observe.onCompleted()
        }
        return Disposables.create() }
//   .share()

print("----------------------1")
signal.bind { (i) in
    print("1", i)
}.disposed(by: bag)

print("----------------------2")
signal.bind { (i) in
    print("2", i)
}.disposed(by: bag)

DispatchQueue.after(.seconds(2)) {
    print("----------------------3")
    signal.bind { (i) in
        print("3", i)
    }.disposed(by: self.bag)
}

normal

----------------------1
---------------------------new
1 1577239849.062496
----------------------2
---------------------------new
2 1577239849.065346
1 1577239850.124456
---------------------------done 1
2 1577239850.1247
---------------------------done 2
----------------------3
---------------------------new
3 1577239851.224869
3 1577239852.257911
---------------------------done 3

1.有订阅后即开始发送 各自生成自己的信号流
2.信号未结束 跟进订阅: 产生新信号流
3.信号已结束 跟进订阅: 产生新信号流

share()

share(replay: 0, scope: .whileConnected)

----------------------1
---------------------------new
1 1577239922.555546
----------------------2
1 1577239923.576826
2 1577239923.576826
---------------------------done 1
----------------------3
---------------------------new
3 1577239924.603504
3 1577239925.6578069
---------------------------done 2

1.有订阅后即开始发送 共享
2.信号未结束 跟进订阅: 同步等待
3.信号已结束 跟进订阅: 产生新信号流

share(replay: 1, scope: .whileConnected)

----------------------1
---------------------------new
1 1577239968.2372909
----------------------2
2 1577239968.2372909
1 1577239969.295541
2 1577239969.295541
---------------------------done 1
----------------------3
---------------------------new
3 1577239970.363443
3 1577239971.385394
---------------------------done 2

1.有订阅后即开始发送 共享
2.信号未结束 跟进订阅: 补发之前一次 同步等待
3.信号已结束 跟进订阅: 产生新信号流

share(replay: 1, scope: .forever)

热 执行一次链式调用 后订阅补发一次

----------------------1
---------------------------new
1 1577240006.015055
----------------------2
2 1577240006.015055
1 1577240007.106161
2 1577240007.106161
---------------------------done 1
----------------------3
3 1577240007.106161

1.有订阅后即开始发送 共享
2.信号未结束 跟进订阅: 补发之前一次 同步等待
3.信号已结束 跟进订阅: 补发之前一次

Table of Contents