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
Transcript
30 slides
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スポンサーとして参加します 当日ブースで同人誌を無料配布します
More Decks by Yoshihisa
See All by Yoshihisa
Android Architecture Componentsを使ってリファクタリングした話
yoshihisa
0
1.8k
Other Decks in Technology
See All in Technology
IP重複は"ランディングVPC"で受け止める ― PrivateLink と Transit Gatewayによるマルチテナント接続基盤の設計
hatahata021
3
250
アラートを見直していたら営業組織への理解にちょっとだけ繋がった話
z63d
1
210
Quick Automate | 目指せ Quick マスター 12
inariku
PRO
1
130
AWS Emulator を評価する: 手元で動く AWS の API の仕組み、始め方、確かめ方
inariku
PRO
2
750
HTTP通信に使おうとすると実はめんどくさいfile_get_contents
maguroalternative
0
200
cdkdがどんだけ早いか、実際に動かして見てみよう
smt7174
1
130
Community Over Code Glasgow 2026 + Lakehouse Day recap
lycorptech_jp
PRO
1
130
Where Should Platform Complexity Live? Lessons from a Cross-Team PR Preview Platform
taiki45
0
140
これからのデータエンジニアの役割について考える / Rethinking the Role of Data Engineers in the AI Era
okiyuki99
1
530
SSFの現在地 ― CAEP/RISCからAI Identityへ
oidfj
0
470
LLM 基盤と kagent v0 を全部 k8s に入れてみた / Deploy both LLM and kagent v0 in k8s
kota2and3kan
0
240
Agent Harnessはどう作る?事例から学ぶAI Agentの設計思想
sergicalsix
0
340
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
225
10k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
33
5.8k
Unsuck your backbone
ammeep
672
58k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
3k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.8k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
770
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
640
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
350
Evolving SEO for Evolving Search Engines
ryanjones
0
360
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.9k
How Software Deployment tools have changed in the past 20 years
geshan
3
35k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
380