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
550
Apple HIG 正式名称クイズ結果発表 / HIG Quiz Result
usamik26
0
130
ゆめみ大技林製作委員会の立ち上げの話 / daigirin project
usamik26
0
290
@ViewLoadingプロパティラッパの紹介と自前で実装する方法 / @ViewLoading property wrapper implementation
usamik26
0
440
これからUICollectionViewを実践活用する人のためのガイド / Guide to UICollectionView
usamik26
1
700
Xcodeとの最近の付き合い方のはなし / Approach To Xcode
usamik26
2
630
UICollectionView Compositional Layout
usamik26
0
700
Coding Swift with Visual Studio Code and Docker
usamik26
0
460
Swift Extension for Visual Studio Code
usamik26
2
940
Other Decks in Programming
See All in Programming
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
330
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
180
return文におけるstd::moveについて
onihusube
1
1.3k
Recoilを剥がしている話
kirik
5
7.2k
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1.1k
テストコード書いてみませんか?
onopon
2
210
Jakarta EE meets AI
ivargrimstad
0
280
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
810
PHPで学ぶプログラミングの教訓 / Lessons in Programming Learned through PHP
nrslib
4
390
Mermaid x AST x 生成AI = コードとドキュメントの完全同期への道
shibuyamizuho
1
260
Featured
See All Featured
Building Your Own Lightsaber
phodgson
103
6.1k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.6k
Learning to Love Humans: Emotional Interface Design
aarron
274
40k
The Invisible Side of Design
smashingmag
298
50k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
Building Better People: How to give real-time feedback that sticks.
wjessup
366
19k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
Speed Design
sergeychernyshev
25
670
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
29
910
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
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 ʹͰ͖Δ