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でTwitterの ライブラリを作ってる話 / RxTwift
Search
Mihyaeru
March 12, 2016
Technology
4
710
RxSwiftでTwitterの ライブラリを作ってる話 / RxTwift
RxSwiftでTwitterの ライブラリを作ってる話
Mihyaeru
March 12, 2016
Tweet
Share
More Decks by Mihyaeru
See All by Mihyaeru
会計 freee バックエンドの今後 / freee backend api
mihyaeru21
8
24k
Railsで実装されている機能のマイクロサービスへの切り離し / split to microservices from rails
mihyaeru21
1
750
WebエンジニアのためのMySQLデータ構造基礎(再) / mysql data structure for web developer 2
mihyaeru21
0
260
WebエンジニアのためのMySQLデータ構造基礎 / mysql data structure for web developer
mihyaeru21
1
630
WebエンジニアのためのMySQL入門 / learning mysql for web developer
mihyaeru21
7
2.6k
入門ReactiveX / about ReactiveX
mihyaeru21
3
420
RxSwiftと愉快な仲間たち / RxSwift with Units
mihyaeru21
4
2.7k
CrystalでTwitter botを作ってみた / Create a Twitter bot in Crystal
mihyaeru21
0
1.3k
Gotanda.pm #1
mihyaeru21
1
150
Other Decks in Technology
See All in Technology
AWS Media Services 最新サービスアップデート 2024
eijikominami
0
200
10XにおけるData Contractの導入について: Data Contract事例共有会
10xinc
6
620
Introduction to Works of ML Engineer in LY Corporation
lycorp_recruit_jp
0
110
TypeScript、上達の瞬間
sadnessojisan
46
13k
Amazon CloudWatch Network Monitor のススメ
yuki_ink
1
200
これまでの計測・開発・デプロイ方法全部見せます! / Findy ISUCON 2024-11-14
tohutohu
3
370
透過型SMTPプロキシによる送信メールの可観測性向上: Update Edition / Improved observability of outgoing emails with transparent smtp proxy: Update edition
linyows
2
210
Evangelismo técnico: ¿qué, cómo y por qué?
trishagee
0
360
【Startup CTO of the Year 2024 / Audience Award】アセンド取締役CTO 丹羽健
niwatakeru
0
990
TanStack Routerに移行するのかい しないのかい、どっちなんだい! / Are you going to migrate to TanStack Router or not? Which one is it?
kaminashi
0
580
フルカイテン株式会社 採用資料
fullkaiten
0
40k
Incident Response Practices: Waroom's Features and Future Challenges
rrreeeyyy
0
160
Featured
See All Featured
Into the Great Unknown - MozCon
thekraken
32
1.5k
Raft: Consensus for Rubyists
vanstee
136
6.6k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
The Pragmatic Product Professional
lauravandoore
31
6.3k
Designing the Hi-DPI Web
ddemaree
280
34k
Scaling GitHub
holman
458
140k
A Philosophy of Restraint
colly
203
16k
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
Transcript
RxSwiftͰTwitterͷ ϥΠϒϥϦΛ࡞ͬͯΔ (͍͍ͩͨ)৽ଔΤϯδχΞ͚ٕज़ަྲྀձ vol.6 on 2016/03/12 @mihyaeru21
͓લɺ୭Α • ϛώϟΤϧ @mihyaeru21 • Swift͓͡͞Μ • Kotlin͓͡͞Μ • ͱ͖Ͳ͖PerlϚϯ
RxSwift + Twitter
RxTwift
RxTwift • RxSwiftϕʔε • ґଘؚΊͯSwiftͷΈ • ܕܕܕ • https://github.com/mihyaeru21/RxTwift
͖ͯͱ͏ͳઆ໌ • TwitterͷAPIͱରԠͨ͠ΠϯλϑΣʔε • ApiΫϥεɺ͍ͣΕRestApiʹ໊લ͕มΘΓͦ͏ let api = RxTwift.Api(…) api.get.statuses.homeTimeline()
GET /statuses/home_timeline
͖ͯͱ͏ͳઆ໌ • શͯͷAPIϝιουObservableΛฦ͢ api.get.statuses.homeTimeline() Observable<Tweet>
͖ͯͱ͏ͳઆ໌ • ObservableͷதJSONͰͳ͍ΦϒδΣΫτ api.get.statuses.homeTimeline() .subscribeNext { tweet in print("\(tweet.user.name)\n\(tweet.text)") }
͖ͯͱ͏ͳઆ໌ • ObservableͷதJSONͰͳ͍ΦϒδΣΫτ api.get.statuses.homeTimeline() .subscribeNext { tweet in print("\(tweet.user.name)\n\(tweet.text)") }
ίʔυิͰ͖Δͤʂʂʂ
ͪΐͬͱෳࡶͳྫ • ͋ΔϦετΛऔಘͯ͠ • ͦͷϦετͷϢʔβʢσϑΥϧτ20ਓʣ • ͷ͏ͪɺID͕ۮͷਓͷʢҙຯෆ໌ͳૢ࡞͚ͩͲྫ͔ͩΒڐͯ͠ʣ • λΠϜϥΠϯΛऔಘͯ͠ʢAPIΛୟ͖·͘Δʣ •
දࣔ
Θʔɺ͍͢͝
தͷ
தͷ • OAuth • ΦϒδΣΫτϚοϐϯά • Rx • ެ։APIϝιουͷྫ
OAuth • OAuthΛSwiftͰ࣮͍ͯ͠ΔϥΠϒϥϦ͋Δ • https://github.com/OAuthSwift/OAuthSwift • ͦΜͳʹଟػೳඞཁͳ͔ͬͨ • ετϦʔϛϯάʹରԠͰ͖ͳͦ͏ •
ͱ͍͏Θ͚Ͱࣗ࡞ • HMAC-SHA1͚ͩCryptoSwiftʹґଘ • https://github.com/krzyzanowskim/CryptoSwift • ඞཁͳػೳΑΓଟػೳ͗͢Δ͔Βґଘ͔Β֎͍ͨ͠
OAuth: ೝূͷϔομΛ࡞Δ෦
OAuth • Ϧετॲཧͱ͔จࣈྻॲཧ͕݁ʹॻ͖͍͢ • ʢൺֱରɿObjective-Cʣ • ྫ֎ॲཧnilΛฦ͚ͩ͢ͷखൈ͖ • खൈ͖͚ͩͲ͜Ε͘Β͍ͷॲཧʹे
ΦϒδΣΫτϚοϐϯά • Argo: https://github.com/thoughtbot/Argo • Swift͚ϥΠϒϥϦͰ࠷γϯϓϧʢͳίʔυʹͳΔ
ΦϒδΣΫτϚοϐϯά • README͔ΒҾ༻ struct User { let id: Int let
name: String let email: String? let role: Role let companyName: String let friends: [User] }
ΦϒδΣΫτϚοϐϯά • README͔ΒҾ༻ extension User: Decodable { static func decode(j:
JSON) -> Decoded<User> { return curry(User.init) <^> j <| "id" <*> j <| "name" <*> j <|? "email" <*> j <| "role" <*> j <| ["company", "name"] <*> j <|| "friends" } }
ΦϒδΣΫτϚοϐϯά • มͳه߸ͱ͔ʹ͍ͭͯྑ͍ղઆهࣄ͕͋Δ • http://qiita.com/koher/items/300d89136b515291dac4
ΦϒδΣΫτϚοϐϯά
ΦϒδΣΫτϚοϐϯά ͕ࣜෳࡶ͗ͯ͢ίϯύΠϧͰ͖Ͷʔ͔Β ׂͯͪ͠ΐ
ΦϒδΣΫτϚοϐϯά ࠅ͍͚Ͳಈ͘…
Rx • RxSwift • https://github.com/ReactiveX/RxSwift • ࠷ۙྲྀߦΓʁͷFRP • http://ninjinkun.hatenablog.com/entry/introrxja
Rx • Observable • 1ͭͷPromiseʹෳͷΛྲྀͤͯߴػೳԽͨ͠ײ͡ • ObservableΛ࡞ͬͨஈ֊ͰԿ͠ͳ͍ • ୭͔ʹsubscribe͞ΕΔ͜ͱʹΑͬͯىಈ
Rx: RxTwiftͰ • APIڞ௨ͷϦΫΤετൃߦ෦ • Observable<NSData>
Rx: RxTwiftͰ • JSONจࣈྻΛදݱ͢ΔNSDataΛσίʔυ • ArgoʹΑͬͯΦϒδΣΫτʹม • Observable<Tweet> • Observable<User>
• etc…
ެ։APIϝιουͷ࣮ྫ
ެ։APIϝιουͷ࣮ྫ
client.get(), client.post() • RxTwiftʹ͓͚ΔObservableͷݯྲྀ • Observable.create()ʹΑͬͯੜ • தෳࡶ͔ͩΒׂѪ
ެ։APIϝιουͷ࣮ྫ
Dictionary.createWithNotNil • nilͳཁૉΛল͍ͨDictionaryΛ࡞Δ
ެ։APIϝιουͷ࣮ྫ
Observable<Element>.decode()
ฦΓͷܕͰΦʔόʔϩʔυ ҧ͍͜Ε͚ͩ
ϚοϐϯάରͷܕฦΓ͔Βਪ
ϚοϐϯάରͷܕฦΓ͔Βਪ ͜ΕʹΑͬͯͲͷΦϒδΣΫτ ʹϚοϐϯά͢Δ͔ܾ·Δ
ϚοϐϯάରͷܕฦΓ͔Βਪ ͜ΕʹΑͬͯͲͷΦϒδΣΫτ ʹϚοϐϯά͢Δ͔ܾ·Δ
Observable<Element>.flatMapSequence() • Observable<[T]> Λ Observable<T> ʹม • ετϦʔϜͱͯ͠ѻ͍ͯ͘͢͠Δ • ʢ͜ͷมແ͔͘͢…ʣ
decode()ͷฦΓͷਪ • flatMapSequence() • Observable<[Tweet]> -> Observable<Tweet> • decode() •
Observable<NSData> -> Observable<[Tweet]>
ݱঢ়Ͱ͖͍ͯΔൣғ • OAuthೝূʹΑΔ௨৴ • GET/POST status/* ͷAPI • GET lists/*
ͷAPI • ࣮ࡁΈAPIͷϨεϙϯεʹରԠ͢ΔΦϒδΣΫτ • TwitterଆΤϥʔʹରԠ͢ΔΦϒδΣΫτ
ࠓޙ࣮͢Δൣғ • ೝূͷτʔΫϯऔಘॲཧ • ະ࣮ͷREST API • ετϦʔϛϯάAPI • ύϥϝʔλʹԠͯ͡ϨεϙϯεJSONͷߏ͕
มΘΔ͜ͱͷରԠ • ະ࣮ͷΦϒδΣΫτ