The big picture and overview of Combine

yimajo

Yoshinori Imajo

Posted on September 28, 2020

The big picture and overview of Combine

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

What is reactive programming?

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

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
Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode

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, 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:

💖 💪 🙅 🚩
yimajo
Yoshinori Imajo

Posted on September 28, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related