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
23k
Railsで実装されている機能のマイクロサービスへの切り離し / split to microservices from rails
mihyaeru21
1
730
WebエンジニアのためのMySQLデータ構造基礎(再) / mysql data structure for web developer 2
mihyaeru21
0
250
WebエンジニアのためのMySQLデータ構造基礎 / mysql data structure for web developer
mihyaeru21
1
620
WebエンジニアのためのMySQL入門 / learning mysql for web developer
mihyaeru21
7
2.5k
入門ReactiveX / about ReactiveX
mihyaeru21
3
410
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
watsonx.ai Dojo 環境準備について
oniak3ibm
PRO
0
230
SORACOMで実現するIoTのマルチクラウド対応 - IoTでのクリーンアーキテクチャの実現 -
kenichirokimura
0
380
Creative UIs with Compose: DroidKaigi 2024
chrishorner
1
480
『GRANBLUE FANTASY: Relink』最高の「没入感」を実現するカットシーン制作手法とそれを支える技術
cygames
0
100
社内の学びの場・コミュニティ形成とエンジニア同士のリレーションシップ構築/devreljapan2024
nishiuma
3
280
Fediverse Discovery Providers overview
andypiper
0
160
サーバー管理しないサーバーサービスManaged DevOps Pool
kkamegawa
0
130
言葉は感情の近似値である。その感情と言葉の誤差を最小化しよう ~コミュニケーションにおけるアナログ/デジタル変換の課題に立ち向かう~
nktamago
0
190
どこよりも遅めなWinActor Ver.7.5.0 新機能紹介
tamai_63
0
200
ロリポップ! for Gamersを支えるインフラ/lolipop for gamers infrastructure
takumakume
0
130
事前準備が肝!AI活用のための業務改革
layerx
PRO
1
370
突撃! 隣のAmazon Bedrockユーザー 〜YouはどうしてAWSで?〜
minorun365
PRO
3
380
Featured
See All Featured
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.2k
Building a Modern Day E-commerce SEO Strategy
aleyda
36
6.8k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
0
96
Product Roadmaps are Hard
iamctodd
PRO
48
10k
Atom: Resistance is Futile
akmur
261
25k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
45
4.8k
[RailsConf 2023] Rails as a piece of cake
palkan
48
4.6k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
103
48k
Agile that works and the tools we love
rasmusluckow
327
20k
The Cost Of JavaScript in 2023
addyosmani
42
5.6k
The World Runs on Bad Software
bkeepers
PRO
64
11k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
225
22k
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ͷߏ͕
มΘΔ͜ͱͷରԠ • ະ࣮ͷΦϒδΣΫτ