GithubHelp home page GithubHelp logo

cookieysun / iosdc2020-combinearticle Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yimajo/iosdc2020-combinearticle

0.0 0.0 0.0 141 KB

このドキュメントはiOSアプリ開発者のカンファレンスiOSDC JAPAN2020で採択された『図解で整理するCombineフレームワークの全体像 2ページ』の原稿です。

Home Page: https://fortee.jp/iosdc-japan-2020/proposal/0f03c5b3-47d2-488b-bd2f-2acbea416564

License: MIT License

iosdc2020-combinearticle's Introduction

図解で整理するCombineフレームワークの全体像

はじめに

AppleはWWDC19にてCombineフレームワークを発表しました。このフレームワークによりリアクティブプログラミングおよび宣言的なコーディングが可能になります。本記事ではそのかんたんな概要とCombineの全体像を示します。

リアクティブプログラミングって?

まずはリアクティブプログラミングの説明として、コード1に手続き型のコードがリアルタイムに変更される値に対応していない例を示します。

var a = 5
let b = 10
let flag = a > b
print(a) // false が出力される
a = 20
print(flag) // flagは当然変化せず false のまま出力

次のコード2ではリアクティブプログラミングによりリアルタイムに変更される値に対応している例です。

let a = CurrentValueSubject<Int, Never>(5)
let b = Just(10)   //  10を要素として流すのみ
let publisher = b.combineLatest(a)
  .map { $0 < $1 } //  b < a の場合に true とする
_ = publisher
  .sink { print($0) } // false と true が次々出力される 
a.send(20) // aの要素として20を流すため b を上回り↑

iOSアプリ開発ではそもそもリアルタイムに値の変化を検知する仕組みとして、従来から様々なイベント通知手段が用意されてはいます。しかしこれを他プラットフォームで利用されるような一般化されたリアクティブプログラミング仕様であるReactive Streamsをベースとした公式フレームワークで行えることがCombineのメリットなわけです。

Publisher

要素をイベントとして通知する概念を示したものがPublisherです。このPublisherという名前はReactiveStreams仕様そのままであり、その仕様に沿っていないRxSwiftではObservableに相当します。

Subscriber

Publisherのイベントを処理する役割としてプロトコルとして用意されています。直接Subscriberを利用するよりも、Publisherのプロトコルエクステンションとしてイベントを処理するsinkメソッドなどが用意されており、Subscriberを間接的に利用することが多いはずです。このSubscriberという名前についてもReactiveStreams仕様をベースとしています。RxSwiftには同様の概念としてObserverが存在します。

Subscription

SubscriberからPublisherへ通知を制御するために存在しており、ReactiveStreams仕様にあるバックプレッシャーを実現することができます。RxSwiftには存在しません。

Publishers

Mapなどのオペレーターが利用する様々な型がPublishersとして列挙されています。Publisherに準拠しており、オペレーターはストリームを処理し列挙されているそれぞれの型へ変換します。

Subject

外部からイベントを発生させられる自由度が高いPublisherです。2つのSubjectが用意されておりCurrentValueSubjectは初期値を持ちます。

Subscribers

Subscriberとして機能する型の名前空間として用意されています。

おわりに

SwiftUIが開発の全盛となっても、@PublishedなどのプロパティラッパーによりCombineを意識しない方法が用意されているため、Combineをなるべく使わないという手もあります。しかし、アプリの仕様は日々複雑になるものであり、そういった際にCombineをより深く知りたくなった際、本記事が役に立つ日があるかもしれません。

参考

iosdc2020-combinearticle's People

Contributors

yimajo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.