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
0
4.6k
OkHttp3+Retrofit2+...
OkHttp3とRetrofit2そしてコルーチンを組み合わせて非同期通信を実現するお話
Yoshihisa
February 01, 2018
Tweet
Share
More Decks by Yoshihisa
See All by Yoshihisa
Android Architecture Componentsを使ってリファクタリングした話
yoshihisa
0
1.7k
Other Decks in Technology
See All in Technology
Bedrock AgentCore Memoryの新機能 (Episode) を試してみた / try Bedrock AgentCore Memory Episodic functionarity
hoshi7_n
2
2k
Kiro を用いたペアプロのススメ
taikis
4
1.9k
半年で、AIゼロ知識から AI中心開発組織の変革担当に至るまで
rfdnxbro
0
150
[Data & AI Summit '25 Fall] AIでデータ活用を進化させる!Google Cloudで作るデータ活用の未来
kirimaru
0
4k
M&Aで拡大し続けるGENDAのデータ活用を促すためのDatabricks権限管理 / AEON TECH HUB #22
genda
0
280
Connection-based OAuthから学ぶOAuth for AI Agents
flatt_security
0
400
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
9.9k
Agent Skillsがハーネスの垣根を超える日
gotalab555
6
4.6k
通勤手当申請チェックエージェント開発のリアル
whisaiyo
3
520
松尾研LLM講座2025 応用編Day3「軽量化」 講義資料
aratako
11
4.5k
モダンデータスタックの理想と現実の間で~1.3億人Vポイントデータ基盤の現在地とこれから~
taromatsui_cccmkhd
2
270
Cloud WAN MCP Serverから考える新しいネットワーク運用 / 20251228 Masaki Okuda
shift_evolve
PRO
0
110
Featured
See All Featured
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
0
260
The #1 spot is gone: here's how to win anyway
tamaranovitovic
1
870
Chasing Engaging Ingredients in Design
codingconduct
0
89
Design in an AI World
tapps
0
100
Building Applications with DynamoDB
mza
96
6.9k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
0
290
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.5k
The Curious Case for Waylosing
cassininazir
0
200
How to make the Groovebox
asonas
2
1.8k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
196
70k
YesSQL, Process and Tooling at Scale
rocio
174
15k
Between Models and Reality
mayunak
0
150
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スポンサーとして参加します 当日ブースで同人誌を無料配布します