Combine in Practice
Show Apple's description.
Expand your knowledge of Combine, Apple's new unified, declarative framework for processing values over time. Learn about how to correctly handle errors, schedule work and integrate Combine into your app today.
In this session more Combine operators are introduced:
Publisherfor when you already have a value that you want to publish. It’s called
Just, as in just publish this value.
- Describes when and where a particular event is delivered
- Supported by
- Delay: defers the delivery of an event until some future time.
- Throttle: guarantees that events are delivered no faster than a specified rate.
Receive(on:): guarantees that downstream received events will be delivered on a particular thread or queue.
Subscribers have three functions:
receive(subscription:)when subscribing the publisher will call this function exactly once
receive(_:)the publisher will provide 0 or more values via this method
receive(completion:)the publisher can eventually provide a completion only once, meaning that it has completed or a failure has risen
receive(completion:) is called, no further value will be forwarded.
Just provide a closure and now for every value received, the closure is going to get called with the value.
Sink will return a
canceller, which is like a token where we can call cancel when we no longer want to get new values.
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 calling
Passthrough, however it stores the last value, so new subscribers have an opportunity to catch up
- SwiftUI owns the subscriber
- We only need to provide a publisher
@BindableObjects in SwiftUI have a single associated type.
- It's a Publisher that is constrained to never fail.
@ObjectBindingallows 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 the
Viewowns the model anyway, so we can access it from there instead.
- Adds a publisher to a property
- Access the the published via wrapped value (in the example below,
$passwordis the publisher aka wrapped value)
Advertise the exact contract we want for our API boundary and hide all the implementation details along the way.
Further example with Future:
- What is returned after creating a subscriber
- This class automatically calls