Combine — Publisher Subscriber
2 min readFeb 20, 2023
Subscriber
Subscribers have three functions:
receive(subscription:)
when subscribing the publisher will call this function exactly oncereceive(_:)
the publisher will provide 0 or more values via this methodreceive(completion:)
the publisher can eventually provide a completion only once, meaning that it has completed or a failure has risen
Once receive(completion:)
is called, no further value will be forwarded.
Special Subscriptions
Sink
Just provide a closure and now for every value received, the closure is going to get called with the value.
This Sink
will return a canceller
, which is like a token where we can call cancel when we no longer want to get new values.
Subjects
Behaves like Publisher and Subscriber.
Supporting multi casting a single value (broadcast to multiple subscribers)
Two kinds of subjects:
Passthrough
: doesn’t store any value, therefore we’ll get a value only once a new one will be sentWe can inject a passthrough subjects in a stream by callingonePublisher.share()
CurrentValue
: likePassthrough
, however it stores the last value, so new subscribers have an opportunity to catch up
SwiftUI
- SwiftUI owns the subscriber
- We only need to provide a publisher
@BindableObject
@BindableObject
s in SwiftUI have a single associated type.- It’s a Publisher that is constrained to never fail.
@ObjectBinding
allows SwiftUI to automatically discover and subscribe to our Publisher.- SwiftUI will automatically generate a new body whenever we signal that the model has changed.
- Note how the “combine upstream” value is
Void
, we do not use as theView
owns the model anyway, so we can access it from there instead.
@Published
- Adds a publisher to a property
- Access the published via wrapped value (in the example below,
$password
is the publisher aka wrapped value)
eraseToAnyPublisher
Advertise the exact contract we want for our API boundary and hide all the implementation details along the way.
AnyCancellable
- What is returned after creating a subscriber
- This class automatically calls
cancel()
ondeinit
.