Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Rxのストリームを 感じるために大切なこと
Search
tofu_san0000
April 06, 2019
Programming
110
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rxのストリームを 感じるために大切なこと
Web技術勉強会#01のLT資料です。
RxSwiftを学んだので、Rxについての概要を少しまとめた緩々の内容となっています。
tofu_san0000
April 06, 2019
More Decks by tofu_san0000
See All by tofu_san0000
RxSwiftでMVVMパターン
tofu_san0000
0
390
Other Decks in Programming
See All in Programming
Claspは野良GASの夢をみるか
takter00
0
200
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
4.3k
Contextとはなにか
chiroruxx
1
330
Lessons from Spec-Driven Development
simas
PRO
0
210
DynamoDBには集計系のクエリがないけどなんとかしたい
musan
1
180
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
250
Semantic Version 単位で戦略を柔軟に変えて、パッケージアップデートを自動化する
daitasu
1
260
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
13k
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.7k
AIで効率化できた業務・日常
ochtum
0
140
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
200
Inside Stream API
skrb
1
740
Featured
See All Featured
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
340
Code Review Best Practice
trishagee
74
20k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
360
A Tale of Four Properties
chriscoyier
163
24k
Tell your own story through comics
letsgokoyo
1
960
The Pragmatic Product Professional
lauravandoore
37
7.3k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.6k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
290
How to Ace a Technical Interview
jacobian
281
24k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.9k
Fireside Chat
paigeccino
42
4k
Transcript
Rxのストリームを 感じるために大切なこと 〜RxSwift編〜 1 @Web技術勉強会#01 2019/04/06(土) @tofu_san0000
2 @tofu_san0000 豆腐: 木綿派 エディタ: Vim、PHPStorm、XCode 言語: Swift、PHP、Vue.js
3 アジェンダ • RxSwiftとは • RxSwiftの基本 • ストリームの操作 • まとめ
4 アジェンダ • RxSwiftとは • RxSwiftの基本 • ストリームの操作 • まとめ
• 各言語で実装されている「Rx」のSwift向けライブラリ • RxKotlin、RxJava、RxJS、RxPHPなどがある 5 RxSwiftとは
6 RxSwiftとは > Rxとは • Rx ◦ Reactive Programming ◦
Extensions • Rxは以下の3つを組み合わせたもの ◦ Observableパターン ◦ Iteratorパターン ◦ 関数型プログラミング
• あらゆるデータをストリームとして処理できる • 非同期処理がシンプルに書ける ◦ コールバック地獄からの解放 • 覚えればRx化した各言語が使いやすくなる ◦ 例)
RxKotlin、RxJava、RxJS 7 RxSwiftとは > Rxのメリット
8 アジェンダ • RxSwiftとは • Rxの基本 • ストリームの操作 • まとめ
• 基本は以下の3つを覚えれる ◦ Observable ◦ Subject ◦ Disposable 9 Rxの基本
• 監視することができる対象 • 監視してデータ(イベント)を受け取ることができる • Observableはストリーム(Stream)とも呼ぶ 10 Rxの基本 > Observable
• Observable.just() ◦ 例: Observable.just(10) • Observable.of() ◦ Observable.of(“a”, “b”,
“c”) • Observable.from() ◦ Observable.from([1, 2, 3]) 11 Rxの基本 > Observable > ストリームの生成
• next ◦ データが正しく流れてきたというイベント ◦ 任意の型のデータを引数にもつ • error ◦ エラー(例外)で異常停止した時に流れるイベント
◦ 発生したエラーを引数にもつ • completed ◦ 完了時に発生するイベント ◦ 最後に一度だけ発生する 12 Rxの基本 > Observable > イベント
ストリームはnextでデータを流し、completedで終了する 一度completedが流れたら、next/errorは流せない errorが流れたら、next/error/completedは流せない 13 Rxの基本 > Observable > イベント next
next next next next next completed next next next next completed next next next next error
• ストリームの購読 ◦ ストリームから流れているイベントを処理する • subscribe()メソッドでストリームを購読できる ◦ 購読とは、ストリームのイベントを実行すること 14 Rxの基本
> Observable > ストリームの購読
Observable.of(“a”, “b”, “c”, “d”).subscribe() 15 Rxの基本 > Observable > ストリームの購読
subscribe() a b c d completed c d next next next next next next
• Observerを継承しているのでObservableとして扱える • 任意のデータを流す機能をもったObservable 16 Rxの基本 > Subject
• PublishSubject ◦ 過去のデータを保持しないSubject • BehaviorSubject ◦ 過去のデータの直近1つを保持しているSubject • RepalySubject
◦ 過去のデータを指定するバッファだけ保持するSubject 17 Rxの基本 > Subject > 種類
18 Rxの基本 > Subject > 種類 > PublishSubject completed subscribe()
subscribe() PublishSubject<T>
19 Rxの基本 > Subject > 種類 > BehaviorSubject completed subscribe()
subscribe() BehaviorSubject<T>
• 直訳すると破棄・処分できるもの • Observableをsubscribe(購読)した戻り値 • Disposableに対してdispose()を呼ぶことで 非同期処理のキャンセル • 監視する側としての処理をキャンセル 20
Rxの基本 > Disposable
21 Rxの基本 > Disposable > dispose() subscribe() Disposable.dispose() dispose()
let disposable = Observable.of(“a”, “b”, “c”) .subscribe( x in //
“a”, “b”, “c” onNext: { value in // イベント発生時の処理 }, onError: { error in // エラー発生時の処理 }, onCompleted: { // 完了時の処理 } ) 22 Rxの基本 > コード例
23 アジェンダ • RxSwiftとは • Rxの基本 • ストリームの操作 • まとめ
• Observable、Subjectで作成したストリームを操作できる • ストリームを変換・合成・生成 24 Rxの基本 > オペレータ
• map ◦ ストリームを変換する • filter ◦ ストリームをフィルタする • flatMap
◦ ストリームを変換する ◦ クロージャの返り値がObservable 25 Rxの基本 > オペレータ > ストリームの操作
26 Rxの基本 > オペレータ > ストリームの操作 > map 0 Observable.of(0,
1, 2, 3).map { x in x * 2 } 1 2 3 4 6 0 2
27 Rxの基本 > オペレータ > ストリームの操作 > filter 0 Observable.of(0,
1, 2, 3).filter { x in x > 1 } 1 2 3 2 3
28 Rxの基本>オペレータ>ストリームの操作 > flatMap 0 Observable.of(0, 1, 2, 3).flatMap {
x in Observable.of(x, x * 2) } 1 2 3 2 3 0 1 0 2 4 6
let disposable = Observable.of(1, 2, 3) .filter { x in
> 1 } // 2, 3 .map { x in x * 2 } // 4, 6 .flatMap { x in Observable.of(x, x * 2) } // 4, 8, 6, 12 .subscribe( x in onNext: { value in // イベント発生時の処理 }, onError: { error in // エラー発生時の処理 }, onCompleted: { // 完了時の処理 } ) 29 Rxの基本>オペレータ>ストリームの操作>コード例
30 アジェンダ • RxSwiftとは • Rxの基本 • ストリームの操作 • まとめ
• Observable、Subject、Disposableの3つを覚える • イベント・データは全てRx化できる • イベント・データをストリームとして考える • オペレータを使い、処理をつなげる 31 まとめ