Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Rxのストリームを 感じるために大切なこと
Search
tofu_san0000
April 06, 2019
Programming
0
100
Rxのストリームを 感じるために大切なこと
Web技術勉強会#01のLT資料です。
RxSwiftを学んだので、Rxについての概要を少しまとめた緩々の内容となっています。
tofu_san0000
April 06, 2019
Tweet
Share
More Decks by tofu_san0000
See All by tofu_san0000
RxSwiftでMVVMパターン
tofu_san0000
0
360
Other Decks in Programming
See All in Programming
新卒エンジニアのプルリクエスト with AI駆動
fukunaga2025
0
210
まだ間に合う!Claude Code元年をふりかえる
nogu66
5
800
俺流レスポンシブコーディング 2025
tak_dcxi
14
8.6k
C-Shared Buildで突破するAI Agent バックテストの壁
po3rin
0
380
これだけで丸わかり!LangChain v1.0 アップデートまとめ
os1ma
6
1.8k
Full-Cycle Reactivity in Angular: SignalStore mit Signal Forms und Resources
manfredsteyer
PRO
0
210
30分でDoctrineの仕組みと使い方を完全にマスターする / phpconkagawa 2025 Doctrine
ttskch
3
830
複数人でのCLI/Infrastructure as Codeの暮らしを良くする
shmokmt
5
2.3k
Tinkerbellから学ぶ、Podで DHCPをリッスンする手法
tomokon
0
130
tparseでgo testの出力を見やすくする
utgwkk
1
210
20251127_ぼっちのための懇親会対策会議
kokamoto01_metaps
2
430
AIエージェントを活かすPM術 AI駆動開発の現場から
gyuta
0
400
Featured
See All Featured
Context Engineering - Making Every Token Count
addyosmani
9
500
What's in a price? How to price your products and services
michaelherold
246
12k
Thoughts on Productivity
jonyablonski
73
5k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
253
22k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
24k
Optimising Largest Contentful Paint
csswizardry
37
3.5k
Faster Mobile Websites
deanohume
310
31k
How to train your dragon (web standard)
notwaldorf
97
6.4k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
196
70k
How STYLIGHT went responsive
nonsquared
100
6k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Building Applications with DynamoDB
mza
96
6.8k
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 まとめ