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
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
トークンマネジメントでAIにとって働きやすい環境を実現する
hikaruegashira
0
140
8bit CPU 2026
koba789
6
2.6k
長期運営で肥大化したExcelマスターデータの解消に向けた移行事例
gree_tech
PRO
0
110
Oracle MCP Servers Explained
thatjeffsmith
0
410
【CEDEC2026】Creative Approaches to Localizing the Dialects and Unique Speech of Umamusume: Pretty Derby Characters in English
cygames
PRO
4
26k
AI開発に用いられるHPC技術について
gpuunite_official
0
250
アクセスキーが漏れた日にやるべきこと- 無効化の先にある本当の対応
kazzpapa3
0
200
【GCC2026】TrueHDRIを用いたルックデブ環境とライティングテクニック
bandainamcostudios
PRO
0
240
コーチングの奥義 何もしないテクニック
jinwatanabe
0
150
[potatotips #96] Give Your AI Agent the Flutter Playbook
korodroid
0
140
LLM Internals: 언어 모델의 계보와 알고리즘 진화 (2023~2026)
inureyes
PRO
1
880
DDDのエッセンスを取り入れたAIでの開発
ak2ie
0
210
Featured
See All Featured
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
480
The Invisible Side of Design
smashingmag
301
52k
Designing for humans not robots
tammielis
254
26k
Designing for Timeless Needs
cassininazir
1
450
HDC tutorial
michielstock
2
810
Everyday Curiosity
cassininazir
0
290
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
45k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
4.4k
4 Signs Your Business is Dying
shpigford
187
23k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
250
Done Done
chrislema
186
16k
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スポンサーとして参加します 当日ブースで同人誌を無料配布します