RxSwift (7) 手动启动序列[连接性序列]
来源:互联网 发布:python 编码转换 编辑:程序博客网 时间:2024/06/05 15:34
publish
转化连接序列,订阅时不会发出信号,执行connect方法时,才开始发射信号
func rx_Publish() { let intSequence = Observable<Int>.interval(1, scheduler: MainScheduler.instance) .publish() _ = intSequence .subscribe(onNext: { print("Subscription 1:, Event: \($0)") }) delay(2) { _ = intSequence.connect() } delay(4) { _ = intSequence .subscribe(onNext: { print("Subscription 2:, Event: \($0)") }) } delay(6) { _ = intSequence .subscribe(onNext: { print("Subscription 3:, Event: \($0)") }) }}
动态结果,复制去执行以下才能看到
replay
replay()相对于publish增加了bufferSize指定对元素的缓存大小,这样新加入的订阅者可以获取相应个数的已发送的元素
func rx_replay() {let stopObservable = PublishSubject<Int>() let intSequence = Observable<Int>.interval(1, scheduler: MainScheduler.instance).takeUntil(stopObservable) .replay(5) _ = intSequence .subscribe(onNext: { print("Subscription 1:, Event: \($0)") }) delay(2) { _ = intSequence.connect() } delay(4) { _ = intSequence .subscribe(onNext: { print("Subscription 2:, Event: \($0)") }) } delay(8) { _ = intSequence .subscribe(onNext: { print("Subscription 3:, Event: \($0)") }) }}
打印结果Subscription 1:, Event: 0Subscription 2:, Event: 0Subscription 1:, Event: 1Subscription 2:, Event: 1Subscription 1:, Event: 2Subscription 2:, Event: 2Subscription 1:, Event: 3Subscription 2:, Event: 3Subscription 1:, Event: 4Subscription 2:, Event: 4Subscription 3:, Event: 0//这里开始回放Subscription 3:, Event: 1Subscription 3:, Event: 2Subscription 3:, Event: 3Subscription 3:, Event: 4//回放结束Subscription 1:, Event: 5Subscription 2:, Event: 5Subscription 3:, Event: 5
multicast
multicast()需要传入一个subject,通过subject来管理向订阅者发送消
let subject = PublishSubject<Int>()_ = subject .subscribeNext { print("Subject: \($0)") }let intSequence = Observable<Int>.interval(1, scheduler: MainScheduler.instance) .multicast(subject)_ = intSequence .subscribeNext { print("\tSubscription 1:, Event: \($0)") }delay(2) { intSequence.connect() }delay(4) { _ = intSequence .subscribeNext { print("\tSubscription 2:, Event: \($0)") }}delay(6) { _ = intSequence .subscribeNext { print("\tSubscription 3:, Event: \($0)") }}
打印结果Subject: 0Subscription 1:, Event: 0Subject: 1Subscription 1:, Event: 1Subscription 2:, Event: 1Subject: 2Subscription 1:, Event: 2Subscription 2:, Event: 2Subject: 3Subscription 1:, Event: 3Subscription 2:, Event: 3Subscription 3:, Event: 3Subject: 4Subscription 1:, Event: 4Subscription 2:, Event: 4Subscription 3:, Event: 4Subject: 5Subscription 1:, Event: 5Subscription 2:, Event: 5Subscription 3:, Event: 5
0 0
- RxSwift (7) 手动启动序列[连接性序列]
- ReactiveX序列——RxSwift
- RxSwift (6) 观察量序列转换符
- Oracle 循环插入 手动序列
- tomcat启动序列图
- 触发器、作业、序列、连接
- 关于RxSwift MVVM flatMapLatest 点击事件网络请求失败整个序列结束
- 序列
- 序列
- 序列
- 序列
- 序列
- 序列
- 序列
- 序列
- 序列
- 序列
- 序列
- WebSphere Application Server 8.5 通过SDK文件直接配置JAVA7
- CCF201612-1
- 机器学习 深度学习 计算机视觉 资料汇总
- Java基础知识之多态性
- Eigen使用快速参考
- RxSwift (7) 手动启动序列[连接性序列]
- sqlserver 让一列的值等于 该列的值加上该行序号的写法 (我只是写给自己看的)
- 如何运行Struts2官网最新Demo-
- Linphone3.2.5 debug机制 自定义log日志
- Hibernate框架中的缓存
- lucene分页查询
- 图像仿射,透视变换
- 魅族测试机安装app失败的BUG 测试机的大坑
- Map