The big picture and overview of Combine
Tags: ios, combine, swift
Published: 2020-09-28 07:43:36

---
title: The big picture and overview of Combine
published: true
description: 
tags: ios, combine, swift
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/25qu78kua81bb6dhgw77.png
series: Combine.framework
---

## Introduction

Apple announced the Combine framework at WWDC19. This framework enables reactive programming and declarative coding. This article will give you a quick overview of it and the big picture of Combine.

![combine_overview_en](https://dev-to-uploads.s3.amazonaws.com/i/25qu78kua81bb6dhgw77.png)

## What is reactive programming?

To help you understand reactive programming, here's an example of how procedural code is not real-time.

```swift
var a = 5
let b = 10
let flag = a > b
print(flag) // => false
a = 20
// of course, flag remains unchanged and remains false
print(flag) // => false
```

The following code shows how reactive programming can be used to deal with values that change in real time.


```swift
let a = CurrentValueSubject<Int, Never>(5)
let b = Just(10) // Let 10 be the only element

let publisher = b.combineLatest(a)
  .map { $0 < $1 } // true if b < a

_ = publisher
  .sink { 
    // When the element `a` changes from 10 to 20, 
    // false and true are output one after another.
    print($0)   // => false
                // => true
  } 

a.send(20) // Send 20 as an element of a
```


n iOS app development, various event notification methods are available to detect changes in value in real-time.
However, the advantage of Combine is that this can be done with an official framework based on [ReactiveStreams](https://www.reactive-streams.org/), which is a generalized reactive programming specification used on other platforms.


## Publisher

The concept of notifying an element as an event is called a Publisher. The name Publisher is verbatim from the ReactiveStreams specification and is the equivalent of Observable in RxSwift, which doesn't follow the specification.


## Subscriber

The Subscriber is provided as a protocol for handling Publisher events. Rather than using the Subscriber directly, the Subscriber is often used indirectly as a Publisher protocol extension, such as the sink method that handles events. The name Subscriber is also based on the ReactiveStreams specification; RxSwift has a similar concept in Observer.

## Publishers

The various types used by operators, such as Maps, are enumerated as Publishers, which are Publisher-compliant and allow the operator to process the stream and convert it to each of the enumerated types.

## Subscription 

Combine is partially compliant with the ReactiveStreams specification, which is based on the Reactive Manifesto. The Reactive Manifesto is a system design pattern, which refers to the design of all applications, not just mobile apps like iOS. Therefore, it also takes into consideration the fault-tolerant surnames required for server-side applications. The ReactiveStreams specification, which follows the Reactive Manifesto, exists to control notifications between Publisher and Subscriber and to reduce the load on them. That's why. This is called back pressure. Of course, in addition to reducing the load, the role of a Subscription is to provide a method of control in the form of a specification.

But whether or not you create a Subscription depends on whether you create your own Publisher or Publishers.

## Subject

The Subject is a highly flexible publisher with the ability to generate events externally.CurrentValueSubject has an initial value.

## Subscribers

The Subscribers are provided as a namespace for types that function as Subscribers.

## Resource:

- [Combine UML](https://twitter.com/tobi462/status/1272755992447442945)
- [Getting Started with Combine - Shai Mishali - App Builders 2020](https://youtu.be/R7KgBgvQJ0c)
- [Processing Published Elements with Subscribers](https://developer.apple.com/documentation/combine/processing-published-elements-with-subscribers )