learningcombine's People
learningcombine's Issues
Publisher
Future
Future是一个publisher,它最终会生成一个值,并且结束或者直接失败。当有数据可使用或者生成了错误之后,Future会调用闭包,然后将数据发送出去。通过查看Future的文档,我们可以看到
final public class Future<Output, Failure> : Publisher where Failure: Error {
public typealias Promise = (Result<Output, Failure>) -> Void
...
}
Promise是一个闭包的类型重命名,它接收一个包含数据或者错误的Result。
Future并不会重复执行promise闭包,它会共享或者重现之前的输出
关于异步编程的一些概念
异步编程的本质是响应未来的事件流。
在异步编程中,一个事件及其对应的数据被发布出来,最后被订阅者消化和使用。期间这些事件和数据需要通过一系列操作变形,成为我们最终需要的事件和数据。
Combine中负责发布事件的是Publisher,负责订阅事件的是Subscriber,负责转换事件和数据的是Operator
Publisher的协议声明及其最重要的一个扩展代码:
public protocol Publisher {
// publisher生成的值的类型
associatedtype Output
// publisher课程产生的错误类型,如果是Never的话,表明这个publisher永远不会生成错误
associatedtype Failure: Error
// 订阅者通过调用publisher的subscribe(_:)方法来将二者绑定在一起
func receive<S>(subscriber: S) where S: Subscriber, Self.Failure == S.Failure, Self.Output == S.Input
}
extension Publisher {
// subscribe(_:)的实现会调用receive(_:)以此来将subscriber和publisher进行关联,也就是创建一个订阅关系
public func subscribe<S>(_ subscriber: S) where S: Subscriber, Self.Failure == S.Failure, Self.Output == S.Input
}
Subscriber协议声明:
public protocol Subscriber: CustomCombineIdentifierConvertible {
// subscriber接收的值类型
associatedtype Input
// subscriber接收的错误类型,如果subscriber不接收任何错误的话,那么使用Never
associatedtype Failure: Error
// publisher通过调用subscriber的receive(subscription:)方法来将subscription传递给subscriber
func receive(subscription: Subscription)
// publisher调用subscriber的receive(_:)来将它刚刚生成的新值发送给subscriber
func receive(_ input: Self.Input) -> Subscribers.Demand
// publisher停止发送数据或者因为错误终止时,会调用subscriber的receive(completion:)告诉subscriber
func receive(completion: Subscribers.Completion<Self.Failure>)
}
将publisher和subscriber链接在一起的是subscription,下面是Subscription的协议:
public protocol Subscription: Cancellable, CustomCombineIdentifierConvertible {
// subscriber会调用request(_:)时指定的最大数量或无限制,来表明它是否愿意接受更多的数据
func request(_ demand: Subscribers.Demand)
}
Subscriber协议中receive(subscription:)和receive(input)两个方法中控制接收数量的关系
Publisher和Subscriber
- Publisher将一段时间内的一系列值以同步或异步的方式传递给一个或多个订阅者
- subscriber可以通过对publisher进行订阅来接收数据,但是subscriber的输入类型和错误类型必须要和publisher的输出类型和错误类型一致
- Publisher内置了两个操作符用于对publisher对象进行订阅,sink(::)和assign(to:on:)
- subscriber在每次接收到数据的时候可以增加接收数据的数量,但是只能增加,不能减少
- 当不需要监听之后,需要取消subscription来清理资源及避免其他问题出现
- Future对象可以在某个时间点异步的接收单个数据
关于Combine的自问自答
Subscriber的意义?
除了使用Publisher的sink()或assign()方法之外,还可以使用Subscriber实现对Publisher的订阅。
两者的区别在于:
- sink()和assign()是Publisher协议的扩展方法,这两个方法只能简单的对publisher进行监听。这两个方法拥有是否取消订阅的话语权。
- Subscriber协议里面订阅的三个方法,receive(subscription)和receive(input)这两个方法控制接收的数据的数量,而receive(completion)方法监听publisher是否停止发送数据,因此是否取消订阅依赖于publisher
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.