COMBINE — X — FILE

INTRODUCTION

PRECONDITION

SUBSCRIPTION FLOW

Publisher-Subscriber dance
The back pressure

MARBLE DIAGRAMS

Marble diagrams convention

HOT PUBLISHER OR COLD PUBLISHER?

SUBSCRIPTION SHADOW GRAPH AND EXPERIMENTS

Resubscribe mechanism
Sharing the publisher

Despite the programmer creating a single graph of Publishers, there is a shadow graph of other instances that really performs the value processing and sending. We can see this shadow graph in the last function in Subscriber protocol.
`func receive(subscription: Subscription)`
Every Publisher in your graph is shadowed by one instance of Subscription per active Subscriber.

FLATMAP

Transforms all elements from an upstream publisher into a new publisher up to a maximum number of publishers you specify.

This means that when you map over a publisher you transform its published values one by one. Using compactMap leads to the omission of nil from the published values. If publishers in Combine are analogous to collections when using map and compactMap, then publishers that we can flatten nested publishers with flatMap.

Metastream
flatMap in action
Cap the demand
switchToLatest() in action

DISPOSE BAG AND TRICKS

Courtesy Matt Neuburg Understanding Combine
extension Publisher { 
func customPrint(_ prefix: String = “”, to: TextOutputStream? = nil, isEnabled: Bool = true) -> AnyPublisher<Self.Output, Self.Failure> {
if isEnabled {
return print(prefix, to: to).eraseToAnyPublisher()
}
return AnyPublisher(self)
}
}

CONCLUSIONS

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store