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
RxSwift Observable.create
Search
USAMI Kosuke
October 26, 2016
Programming
0
1.1k
RxSwift Observable.create
http://kanmoba.connpass.com/event/42374/
USAMI Kosuke
October 26, 2016
Tweet
Share
More Decks by USAMI Kosuke
See All by USAMI Kosuke
Onsager代数とその周辺 / Onsager algebra tsudoi
usamik26
0
530
Apple HIG 正式名称クイズ結果発表 / HIG Quiz Result
usamik26
0
110
ゆめみ大技林製作委員会の立ち上げの話 / daigirin project
usamik26
0
280
@ViewLoadingプロパティラッパの紹介と自前で実装する方法 / @ViewLoading property wrapper implementation
usamik26
0
430
これからUICollectionViewを実践活用する人のためのガイド / Guide to UICollectionView
usamik26
1
690
Xcodeとの最近の付き合い方のはなし / Approach To Xcode
usamik26
2
610
UICollectionView Compositional Layout
usamik26
0
680
Coding Swift with Visual Studio Code and Docker
usamik26
0
450
Swift Extension for Visual Studio Code
usamik26
2
910
Other Decks in Programming
See All in Programming
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
CSC509 Lecture 09
javiergs
PRO
0
140
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
890
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
120
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
watsonx.ai Dojo #4 生成AIを使ったアプリ開発、応用編
oniak3ibm
PRO
1
140
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
930
Figma Dev Modeで変わる!Flutterの開発体験
watanave
0
140
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
190
Quine, Polyglot, 良いコード
qnighy
4
650
RubyLSPのマルチバイト文字対応
notfounds
0
120
Featured
See All Featured
Measuring & Analyzing Core Web Vitals
bluesmoon
4
130
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Practical Orchestrator
shlominoach
186
10k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
Making Projects Easy
brettharned
115
5.9k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
10 Git Anti Patterns You Should be Aware of
lemiorhan
655
59k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Navigating Team Friction
lara
183
14k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Transcript
RxSwift Observable ͷ࡞Γํ USAMI Kosuke (Fenrir Inc.)
RxSwift 3 released » For Swift 3 » ಛʹɺ໋໊ϧʔϧ͕େ෯ʹมΘ͍ͬͯΔ »
Swift 2 ܥͰ RxSwift 2 ܥΛ͏
RxSwift ͓͞Β͍
Observable ͱ » ҎԼͷ3छྨͷͷ͕ྲྀΕͯ͘ΔඇಉظετϦʔϜ » .next(Element) » .error(Error)ʢετϦʔϜऴྃʣ » .completedʢετϦʔϜऴྃʣ
Observable Λ͏ : جຊܗ hogeObservable .subscribe(onNext: { element in print(element)
}) .addDisposableTo(disposeBag)
Observable Λ͏ : Operator hogeObservable .catchError { ... } .filter
{ ... } .map { ... } ...
ΑΓৄ͘͠ҎԼ͕Φεεϝ » Rx.playground » RxExample.xcodeproj » RxSwift ϦϙδτϦʹ͋Δ
༻ҙ͞Ε͍ͯΔ Observable » UI ύʔπͷ Observable ͍͍ͩͨ༻ҙ͞Ε͍ͯΔ » UIButton.rx.tap »
UITextField.rx.text » UILabel.rx.text
୯७ʹΠϕϯτΛྲྀ͢ Observable let variable = Variable("1") variable.value = "2" variable.value
= "3" let subject = PublishSubject<String>() subject.onNext("A") subject.onNext("B")
ͦΕҎ֎ͷ Observable ʁ » ඞཁʹԠͯࣗ͡Ͱ༻ҙ͢Δ
Observable Λ࡞Δ
Observable.create() let hoge = Observable.create { observer in observer.on(.next("hoge")) observer.on(.completed)
return Disposables.create() }
ϙΠϯτ (1) ΤϥʔΛૹΖ͏ » Observable ͷྑ͞Λੜ͔͢ » ޭ࣌ʹ݁Ռ͚ͩʢnextʣɺࣦഊ࣌ʹΤϥʔ͚ͩʢerrorʣ » ݁ՌͱΤϥʔ͕྆ํฦΔ͜ͱͳ͍ͱ͍͏อূ
» ΤϥʔΛࣺͯΔͷ Observer ଆͷࣄ
ϙΠϯτ (1) ΤϥʔΛૹΖ͏ let hoge = Observable.create { [unowned self]
observer in do { let result = try self.hogehoge() observer.on(.next(result)) observer.on(.completed) } catch let error { observer.on(.error(error)) } return Disposables.create() }
ϙΠϯτ (2) ΩϟϯηϧՄೳʹ͠Α͏ » Observable ͷࢹʢsubscribeʣΛΊΔʢdisposeʣ߹ͷରॲ » dispose ͞Εͨ࣌ͷΩϟϯηϧॲཧ Observable
ଆͷࣄ
ϙΠϯτ (2) ΩϟϯηϧՄೳʹ͠Α͏ let hoge = Observable.create { [unowned self]
observer in let task = self.dataTaskWithRequest(request) { ... } task.resume() return Disposables.create { task.cancel() // dispose ࣌ʹ͕࣮͜͜ߦ͞ΕΔ } }
Ԡ༻ : ΩϟϯηϧϙΠϯτΛ࡞Δ let hoge = Observable.create { [unowned self]
observer in self.scheduler.schedule(()) { _ in hogeTask1() // ͜͜ͰΩϟϯηϧͰ͖ΔΑ͏ʹ͍ͨ͠ hogeTask2() } return Disposables.create { ??? } }
Ԡ༻ : ΩϟϯηϧϙΠϯτΛ࡞Δ let hoge = Observable.create { [unowned self]
observer in let cancelDisposable = BooleanDisposable() let disposable = self.scheduler.schedule(()) { _ in hogeTask1() if cancelDisposable.disposed == true { return } hogeTask2() } return CompositeDisposable(disposable, cancelDisposable) }
Observable Λ࡞Δʹ » Ҏ্ͷ͜ͱΛԡ͓͚͍͍͑ͯ͞Μ͡Όͳ͍Ͱ͠ΐ͏͔
Observable Λ࡞Δͱ » Non Rx ͳੈքΛ Rx ͳੈքʹ͢Δ » Observable
͕࡞ΕΔͱɺͳΜͰ Rx ʹͰ͖Δ