Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Rxのストリームを 感じるために大切なこと
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
tofu_san0000
April 06, 2019
Programming
120
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
400
Other Decks in Programming
See All in Programming
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
4.8k
App Storeの外へ──日本のiOSサイドローディング入門 for iOSDC Japan 2026
yuukiw00w
0
280
Are APIs Still Relevant in the AI Era?
soyuka
0
390
AHC070解法紹介
eijirou
0
150
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
300
JPUG勉強会 OSSデータベースの内部構造を理解しよう(第2回)
oga5
0
290
JRuby: Past, Present, and Future
headius
0
220
MVNOの申込からeSIM開通までをiOSアプリでつなぐ- 本人確認・MNP・通信事業者基盤をまたぐ実装
satotakeshi
0
520
手動確認はもう限界 〜XCUITestでCustom URL Schemeの遷移を起動種別ごとに自動テストする〜 / Testing Custom URL Schemes with XCUITest
otouto
0
370
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
820
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
390
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
1k
Featured
See All Featured
The Mindset for Success: Future Career Progression
greggifford
PRO
0
520
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8k
Amusing Abliteration
ianozsvald
1
320
The World Runs on Bad Software
bkeepers
PRO
72
12k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
380
エンジニアに許された特別な時間の終わり
watany
109
250k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
250
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
290
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
69
66k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
340
Context Engineering - Making Every Token Count
addyosmani
9
1.2k
First, design no harm
axbom
PRO
2
1.3k
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 まとめ