25th Jan 2022

Combine Framework in Swift

Mobile App

Written By, Abhishek Bakhai

Overview

Combine was introduced as a new Framework by Apple at WWDC 2019. Combine can be used to simplify your code for dealing with a number of delegates, notifications, completion handler, KVO, networking, and callbacks. It can also be reduced by third-party reactive frameworks(RxSwift) but now Apple has made its own. So, let’s take some basic information about the combined framework.

What is Combine Framework?

A declarative swift API for processing values over time.

Now, maybe you have a question about what is declarative then let’s take an example for the better understanding,

ImperativeDeclarative
Explicit Instructions Describe Outcome
Focus on “How”, how something works
Ex: How’s our system is work
Focus on “What”, Adjust to how humans think and we’re describing what we actually want
Example:
Go to iscon circle, go straight on iscon road, make a right turn at the second stop, go straight find the BSquare - 2, visit the office at 13th-floor
Example:
Go to Yudiz PVT Solution Office located in Ahm

Combine Framework having some core concepts

  • Publisher and Subscriber
  • Operators
  • Subject

What is a Publisher?

A type that can emit a sequence of values over time. It is a protocol that has two associated types Output and Failure

Publisher<Output, Failure: Error>

  • Output: Type of values that the publisher can emit
  • Failure: Strict type of Error can emit

Publishers can emit 3 types of events one is value event and the other two is completion event
Value Event:
Value(Output)

Completion Event:
Finished: Publisher finished without any issues
Failure(Failure): Publisher finished but something went wrong

Any completion event terminates the publisher and no further values will be published

What is a Subscriber?

A publisher is a push-based interface, which delivers its values to one or more Subscribers based on their demand same as a publisher it has two associated types Input and Failure

Subscriber<Input, Failure: Error>

  • Input: Type of values that the subscribers can emit
  • Failure: Strict type of Error can emit

Note: When you connect a subscriber to a publisher, both types must match, i.e. Output to Input, and Failure to Failure.

combine_subscriber

What is a Subscription?

A subscription represents the single connection between a publisher and a subscriber which you can see from the above image and is where most of the “Logical Work” happens.

Publishers provide a subscription to the subscriber using a method called receive(subscription:)

combine_subscription
The subscription pushes events to subscribers using 2 receive event methods
Value Event:
receive(Input)

Completion Event:
receive(completion: .finished)
receive(completion: .failure(Failure))

Subscription kind of encapsulates the work that means that if you don’t need a subscription anymore you can just call “cancel()” or deallocate the subscription. It will release any memory and resources allocated by the attached subscriber.

What are Operators?

Operators let you manipulate upstream (i.e.Publisher and Consumer so up and down) publishers and control the emissions in extremely expressive and flexible ways. Simple, chainable methods with many same named variants in the SSL. (Swift Standard Library)
If we have a publisher and apply some operators to it, we’re going to get back a modified publisher that is transformed by the original publisher.


If combine would be a language, operators are the words.

Operators are publishers themselves. Each operator’s type wraps the previous one in the chain.

What is a Subject

A subject is a special kind of publisher you can manually send values to, which is quite useful for reaching an imperative code. It can kind of be a subscriber and a publisher both.
A subject can be used to “inject” values into a stream, by calling its send(:) method.

Publisher and Subject

  • Publisher, we can subscribe to it but it’s immutable(read-only) we can’t add values onto it.
  • The subject is mutable, which means we can directly send values into this subject and then subscribe to that subject will get these values.

There are two kind of subjects

  • PassthroughSubject : Useful for representing events
    Ex:
    let obj = PassthroughSubject<Void, Never>()
    obj.send()
    Note: New subscribers get any values sent after the subscription.
  • CurrentValueSubject : Useful for representing state
    Ex:
    let isApiCall = CurrentValueSubject<Bool, Never>(false)
    isApiCall.send(true)
    Note: New subscribers get a reply to the current value.

Control flow:

Written By,

iOS Developer at Yudiz Solutions Pvt. Ltd