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
tofu_san0000
April 06, 2019
Programming
130
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
Transcript
31 slides
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 まとめ
More Decks by tofu_san0000
See All by tofu_san0000
RxSwiftでMVVMパターン
tofu_san0000
0
410
Other Decks in Programming
See All in Programming
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
1
320
Lambda durable functionsでHuman in the Loopを楽に…楽に……
har1101
2
310
Secure Your Coding Agent Like It’s Malware (v1.5) 🇬🇧 @Code.talks 2026
richargh
0
150
Materialが消えた日 〜真っ白なFlutterキャンバスにRenderObjectで絵を描く〜
umigishiaoi
0
160
はじめてのPLATEAU
nokonoko1203
1
650
TanStack Vue Query と Pinia Colada から学ぶ非同期状態の型設計
aoshi
0
670
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
310
全ての永続トークンを、生まれる前に消し去りたい!
sylph01
3
1.7k
プロジェクトコードをキレイキレイしよう!暗黙的な参照を読み解く静的解析
ostk0069
0
210
Long Living Tokens, Die, Die, Die! (or: The One Ring is Banned in Modern)
sylph01
0
420
React Native経験者が Flutterを30⽇間触って驚いたこと
3saito
0
180
선언형 햅틱 라이브러리 진동을 소개합니다
l2hyunwoo
1
310
Featured
See All Featured
KATA
mclloyd
PRO
35
16k
Building Better People: How to give real-time feedback that sticks.
wjessup
371
20k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.6k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
520
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.7k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
2
6.4k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.9k
Discover your Explorer Soul
emna__ayadi
2
1.3k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.2k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
880