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
OkHttp3+Retrofit2+...
Search
Yoshihisa
February 01, 2018
Technology
4.7k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
OkHttp3+Retrofit2+...
OkHttp3とRetrofit2そしてコルーチンを組み合わせて非同期通信を実現するお話
Yoshihisa
February 01, 2018
More Decks by Yoshihisa
See All by Yoshihisa
Android Architecture Componentsを使ってリファクタリングした話
yoshihisa
0
1.8k
Other Decks in Technology
See All in Technology
品質と信頼性を地続きにする
grimoh
2
820
Issue 駆動でスペシャリストの意図を届ける、AI 実装のアクセシビリティ向上
thkt
0
120
積み重なった技術負債への挑戦 〜初手としての全社ゴト化〜
techtekt
PRO
0
1.4k
負債のメタファと2026年 / Debt Metaphor in Agentic Engineering Age 202609 Edition
twada
PRO
10
4.3k
AI coding 整合正規方法
philipz
0
360
SREの視点で考えるSIEM活用術 〜AWS環境でのセキュリティ強化〜
cscengineer
PRO
0
130
Goodbye ShellScript, Hello File-based App
shunsock
0
390
え、こんなに早く改修できるの?──新人エンジニアとスクラムマスターの2人が語る、AI×アジャイル開発の現場
ysasago
2
530
おい、エージェントを使って終わらせろ
nwiizo
2
750
サーバーレスをどこまで使う? WebRTC対戦ゲームで選んだVPSとの共存設計
kaidouji85
0
190
時うどん〜Socket.getifaddrsで学ぶネットワーク編 / Tokiudon: The Socket.getifaddrs Edition
coe401_
3
150
作って終わりじゃないサーバーレス 〜9年運用する大規模EC物流API基盤の設計・運用のリアル〜
zozotech
PRO
0
290
Featured
See All Featured
Scaling GitHub
holman
464
140k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.5k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
330
The Spectacular Lies of Maps
axbom
PRO
1
1k
Skip the Path - Find Your Career Trail
mkilby
1
230
The #1 spot is gone: here's how to win anyway
tamaranovitovic
4
1.2k
The Curious Case for Waylosing
cassininazir
1
510
Test your architecture with Archunit
thirion
2
2.4k
It's Worth the Effort
3n
188
29k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
280
Mind Mapping
helmedeiros
1
360
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.6k
Transcript
OkHttp3+Retrofit2+...
• Yoshihisa Takeda • @bomneko_attack • 株式会社Diverse(ミクシィグループ) • YYCというマッチングアプリの開発をしています •
Android開発をはじめてもうすぐ1年
APIを叩くAndroidアプリをつくるとき 通信部分を作り込むときにつかうライブラリ
OkHttp http://square.github.io/okhttp/
Retorofit http://square.github.io/retrofit/
None
None
None
コールバック地獄…!
ReactiveX(Rx)
Observable系(Single,Flowableなど)を返すように書く
Call<T>をRxJava2のObservable系に変換するCall Adapterを指定する
None
コールバック地獄からの解放…!
完
完?
できることならこう書きたくないですか?
Coroutine
OkHttp3+Retrofit2+Coroutine
Coroutine • 「中断・再開が可能な関数」 ◦ とりあえずの理解としては「軽量スレッド」的なもの • Kotlin1.1から実験的に導入された • Coroutinesをゼロから説明すると本1冊書けるぐらいになるので 本発表では導入とRetrofitでの使い方についてのみ
警告 • Kotlin CoroutineはKotlin1.2の時点ではExperimentalで提供 プロダクションへの導入はまだ控えたほうが安全 ◦ 1.3でExperimentalが外れる予定なので勉強をはじめるには良いタイミング ◦ 本資料の組み合わせは Kotlin
1.2 + kotlinx.coroutine 0.21.1
導入 • build.gradleに2つライブラリを追加 ◦ kotlin.coroutines=enable はいらない
Call Adapter • Jake神のCall Adapter ◦ 他にもう1つ見つけたけど今回はこれ ◦ Call<T>をCoroutineのDeferred<T>に変換する
Deferred<T>を返すように書く
Call<T>をCoroutineのDeferred<T>に変換するAdapterを指定
None
レスポンスが返ってくるまで中断 (UIスレッドはブロックされない )
Rxはいらない子になるのか? • No • RxがPromise代わり使われてきたという歴史 • コルーチンもPromiseを同期的に書く手法(async/await)を提供しているので勘違い しやすい • Rxは「リアクティブプログラミングのためのフレームワーク」なのでそもそも比較対象
にならない
まとめ • もうすぐくるかもしれないコルーチン時代に備えて勉強はしておこう • Jakeのライブラリを使えばOkHttpとRetrofitの組み合わせにおいてコルーチンを簡 単に導入でき使える! ◦ もう1つはこれ。どっちを使うかはお好みで … https://github.com/gildor/kotlin-coroutines-retrofit
宣伝 DiverseはDroidKaigi2018のPLATINUMスポンサーとして参加します 当日ブースで同人誌を無料配布します