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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
Family mrubyの進捗
kishima
1
110
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
1
260
週末にAI-DLCを本気で回したら$1,600溶けた
hbashimizu
0
130
Intent as Code
shoppingjaws
6
970
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
0
220
AWS DevOps Agentで インシデント対応をAIに任せたい
honmarkhunt
7
2.8k
アクセシビリティから考える情報設計
high_g_engineer
0
360
個人開発基盤をまるごとCloudflareに引っ越して爆速で総合的体験を向上させた話
tinykitten
0
170
AIを上手に使っていこうとしたら越境せざるを得なくなった話 〜実践1年で見えた境界を越えなければならない理由と進め方〜 / Crossing borders with AI
tomoyakitaura
4
1.1k
更なる可用性を求めて、5年間運用したKotlinのアプリケーションをGoでリプレイスする話
ken_tunc
0
130
LLMは4年分のCompose移行を再現できるのか?実プロダクト279件のXMLで探る自動化の境界線
makun
0
590
AHC070解法紹介
eijirou
0
110
Featured
See All Featured
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.3k
Into the Great Unknown - MozCon
thekraken
41
2.7k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
280
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Balancing Empowerment & Direction
lara
6
1.3k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
390
Testing 201, or: Great Expectations
jmmastey
46
8.3k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4.3k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
600
How GitHub (no longer) Works
holman
316
150k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
490
Context Engineering - Making Every Token Count
addyosmani
9
1.1k
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 まとめ