Combine 框架,从0到1 —— 5.Combine 中的 Subjects
本文首发于 Ficow Shen's Blog,原文地址: Combine 框架,从0到1 —— 5.Combine 中的 Subjects。
内容概览
- 前言
- PassthroughSubject
- CurrentValueSubject
- Subject 作为订阅者
- 常见用法
- 总结
前言
正所谓,工欲善其事,必先利其器。在开始使用 Combine 进行响应式编程之前,建议您先了解 Combine 为您提供的各种发布者(Publishers)、操作符(Operators)、订阅者(Subscribers)。
Subject 是一类比较特殊的发布者,因为它同时也是订阅者。Combine 提供了两类 Subject :PassthroughSubject 和 CurrentValueSubject。
如果您想了解更多 Publishers 的用法和注意事项,可以阅读:Combine 框架,从0到1 —— 5.Combine 提供的发布者(Publishers)
PassthroughSubject
PassthroughSubject 可以向下游订阅者广播发送元素。使用 PassthroughSubject 可以很好地适应命令式编程场景。
如果没有订阅者,或者需求为0,PassthroughSubject 就会丢弃元素。
示例代码:
final class SubjectsDemo {
private var cancellable: AnyCancellable?
private let passThroughtSubject = PassthroughSubject<Int, Never>()
func passThroughtSubjectDemo() {
cancellable = passThroughtSubject
.sink {
print(#function, $0)
}
passThroughtSubject.send(1)
passThroughtSubject.send(2)
passThroughtSubject.send(3)
}
}
输出内容:
passThroughtSubjectDemo() 1
passThroughtSubjectDemo() 2
passThroughtSubjectDemo() 3
CurrentValueSubject
CurrentValueSubject 包装一个值,当这个值发生改变时,它会发布一个新的元素给下游订阅者。
CurrentValueSubject 需要在初始化时提供一个默认值,您可以通过 value 属性访问这个值。在调用 send(_:) 方法发送元素后,这个缓存值也会被更新。
示例代码:
final class SubjectsDemo {
private var cancellable: AnyCancellable?
private let currentValueSubject = CurrentValueSubject<Int, Never>(1)
func currentValueSubjectDemo() {
cancellable = currentValueSubject
.sink { [unowned self] in
print(#function, $0)
print("Value of currentValueSubject:", self.currentValueSubject.value)
}
currentValueSubject.send(2)
currentValueSubject.send(3)
}
}
输出内容:
currentValueSubjectDemo() 1
Value of currentValueSubject: 1
currentValueSubjectDemo() 2
Value of currentValueSubject: 2
currentValueSubjectDemo() 3
Value of currentValueSubject: 3
Subject 作为订阅者
示例代码:
final class SubjectsDemo {
private var cancellable1: AnyCancellable?
private var cancellable2: AnyCancellable?
private let optionalCurrentValueSubject = CurrentValueSubject<Int?, Never>(nil)
private func subjectSubscriber() {
cancellable1 = optionalCurrentValueSubject
.sink {
print(#function, $0)
}
cancellable2 = [1, 2, 3].publisher
.subscribe(optionalCurrentValueSubject)
}
}
optionalCurrentValueSubject 可以作为一个订阅者去订阅序列发布者 [1, 2, 3].publisher 发送的元素。
输出内容:
subjectSubscriber() nil
subjectSubscriber() Optional(1)
subjectSubscriber() Optional(2)
subjectSubscriber() Optional(3)
常见用法
在使用 Subject 时,我们往往不会将其暴露给调用方。这时候,可以使用 eraseToAnyPublisher 操作符来隐藏内部的 Subject。
示例代码:
struct Model {
let id: UUID
let name: String
}
final class ViewModel {
private let modelSubject = CurrentValueSubject<Model?, Never>(nil)
var modelPublisher: AnyPublisher<Model?, Never> {
return modelSubject.eraseToAnyPublisher()
}
func updateName(_ name: String) {
modelSubject.send(.init(id: UUID(), name: name))
}
}
外部调用者无法直接操控 ViewModel 内部的 Subject,这样可以让 ViewModel 更好地面对将来可能的改动。
外部调用者只需要知道 modelPublisher 是 AnyPublisher<Model?, Never> 类型的发布者即可,无论内部采用了 CurrentValueSubject 还是 PassthroughSubject 甚至是其他的发布者。
总结
相比于其他的发布者来说, Subject 是比较容易理解的,而且也是最常用到的。
只可惜,对比 Rx 提供的 Subject,Combine 中的 Subject 无法设置缓冲的大小。也许某天苹果会对此做出调整吧~
Combine 框架,从0到1 —— 5.Combine 中的 Subjects的更多相关文章
- Combine 框架,从0到1 —— 1.核心概念
本文首发于 Ficow Shen's Blog,原文地址: Combine 框架,从0到1 -- 1.核心概念. 内容概览 前言 核心概念 RxSwift Combine 总结 参考内容 ...
- Combine 框架,从0到1 —— 2.通过 ConnectablePublisher 控制何时发布
本文首发于 Ficow Shen's Blog,原文地址: Combine 框架,从0到1 -- 2.通过 ConnectablePublisher 控制何时发布. 内容概览 前言 使用 ma ...
- Combine 框架,从0到1 —— 3.使用 Subscriber 控制发布速度
本文首发于 Ficow Shen's Blog,原文地址: Combine 框架,从0到1 -- 3.使用 Subscriber 控制发布速度. 内容概览 前言 在发布者生产元素时消耗它们 使 ...
- Combine 框架,从0到1 —— 4.在 Combine 中使用通知
本文首发于 Ficow Shen's Blog,原文地址: Combine 框架,从0到1 -- 4.在 Combine 中使用通知. 内容概览 前言 让通知处理代码使用 Combine 总结 ...
- Combine 框架,从0到1 —— 4.在 Combine 中使用计时器
本文首发于 Ficow Shen's Blog,原文地址: Combine 框架,从0到1 -- 4.在 Combine 中使用计时器. 内容概览 前言 使用计时器执行周期性的工作 将计时器转换为计时 ...
- Combine 框架,从0到1 —— 4.在 Combine 中使用 KVO
本文首发于 Ficow Shen's Blog,原文地址: Combine 框架,从0到1 -- 4.在 Combine 中使用 KVO. 内容概览 前言 用 KVO 监控改动 将 KVO 代 ...
- Combine 框架,从0到1 —— 4.在 Combine 中执行异步代码
本文首发于 Ficow Shen's Blog,原文地址: Combine 框架,从0到1 -- 4.在 Combine 中执行异步代码. 内容概览 前言 用 Future 取代回调闭包 用输出类型( ...
- Combine 框架,从0到1 —— 5.Combine 提供的发布者(Publishers)
本文首发于 Ficow Shen's Blog,原文地址: Combine 框架,从0到1 -- 5.Combine 提供的发布者(Publishers). 内容概览 前言 Just Future D ...
- Combine 框架,从0到1 —— 5.Combine 常用操作符
本文首发于 Ficow Shen's Blog,原文地址: Combine 框架,从0到1 -- 5.Combine 常用操作符. 内容概览 前言 print breakpoint handleEve ...
随机推荐
- 如何加速github下载速度
第一步 魔法 第二步 git config --global http.proxy http://127.0.0.1:1080 git config --global https.proxy http ...
- Python | 多线程死锁问题的巧妙解决方法
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是Python专题的第25篇文章,我们一起来聊聊多线程开发当中死锁的问题. 死锁 死锁的原理非常简单,用一句话就可以描述完.就是当多线程 ...
- 面试:为了进阿里,死磕了ThreadLocal内存泄露原因
前言 在分析ThreadLocal导致的内存泄露前,需要普及了解一下内存泄露.强引用与弱引用以及GC回收机制,这样才能更好的分析为什么ThreadLocal会导致内存泄露呢?更重要的是知道该如何避免这 ...
- CSS布局中浮动问题的四种解决方案
一.起因: 子盒子设置浮动之后效果: 由此可见,蓝色的盒子设置浮动之后,因为脱离了标准文档流,它撑不起父盒子的高度,导致父盒子高度塌陷.如果网页中出现了这种问题,会导致我们整个网页的布局紊乱 二.解决 ...
- 阿里大牛教你基于Python的 Selenium自动化测试示例解析
今天给大家讲解的是自动化测试示例的解析,如有不对的地方请多多指教. 自动化测试示例如下: from selenium import webdriver from selenium.webdriver. ...
- python编写汉诺塔 Hanoi
#hanoi.py count = 0 def hanoi(n, src, dst, mid): #src为原1号柱子 dst 目标3号柱子 mid中间2号过渡柱子 global count #对全局 ...
- 网络请求以及网络请求下载图片的工具类 android开发java工具类
package cc.jiusan.www.utils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; ...
- wxmini
微信小游戏架构概览 https://www.jianshu.com/p/02199c35d749 微信小程序:工具配置 project.config.json https://www.cnblogs. ...
- SpringBoot整合Quartz及log4j实例
SpringBoot整合Quartz及log4j实例 因为之前项目中经常会做一些定时Job的东西,所以在此记录一下,目前项目中已经使用elastic-job,这个能相对比Quartz更加简单方便一些, ...
- 当this碰到return会发生什么
当this碰到return时 function fn(params) { this.user = 'fzy' return {} } var a = new fn console.log(a.user ...