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
AndroidXとKotlin Coroutines
Search
Takuji Nishibayashi
May 15, 2019
Technology
0
340
AndroidXとKotlin Coroutines
Takuji Nishibayashi
May 15, 2019
Tweet
Share
More Decks by Takuji Nishibayashi
See All by Takuji Nishibayashi
CameraX使ってみた
takuji31
0
86
kotlinx.datetime 使ってみた
takuji31
0
330
HiltのCustom Componentについて
takuji31
0
190
java.timeをAndroidで使う
takuji31
0
74
KSPを使ってコード生成
takuji31
0
310
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
990
kotlinx.serialization
takuji31
0
550
kanmoba-returns-02.pdf
takuji31
0
180
AndroidXに潜む便利なヤツら
takuji31
0
160
Other Decks in Technology
See All in Technology
Oracle Cloudの生成AIサービスって実際どこまで使えるの? エンジニア目線で試してみた
minorun365
PRO
4
280
【re:Invent 2024 アプデ】 Prompt Routing の紹介
champ
0
150
OpenAIの蒸留機能(Model Distillation)を使用して運用中のLLMのコストを削減する取り組み
pharma_x_tech
4
560
WACATE2024冬セッション資料(ユーザビリティ)
scarletplover
0
210
サイボウズフロントエンドエキスパートチームについて / FrontendExpert Team
cybozuinsideout
PRO
5
38k
Google Cloud で始める Cloud Run 〜AWSとの比較と実例デモで解説〜
risatube
PRO
0
110
20241214_WACATE2024冬_テスト設計技法をチョット俯瞰してみよう
kzsuzuki
3
530
非機能品質を作り込むための実践アーキテクチャ
knih
5
1.5k
LINE Developersプロダクト(LIFF/LINE Login)におけるフロントエンド開発
lycorptech_jp
PRO
0
120
事業貢献を考えるための技術改善の目標設計と改善実績 / Targeted design of technical improvements to consider business contribution and improvement performance
oomatomo
0
100
PHPからGoへのマイグレーション for DMMアフィリエイト
yabakokobayashi
1
170
ずっと昔に Star をつけたはずの思い出せない GitHub リポジトリを見つけたい!
rokuosan
0
150
Featured
See All Featured
The Invisible Side of Design
smashingmag
298
50k
A Tale of Four Properties
chriscoyier
157
23k
Building an army of robots
kneath
302
44k
Java REST API Framework Comparison - PWX 2021
mraible
28
8.3k
Navigating Team Friction
lara
183
15k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Fireside Chat
paigeccino
34
3.1k
Unsuck your backbone
ammeep
669
57k
The Language of Interfaces
destraynor
154
24k
YesSQL, Process and Tooling at Scale
rocio
169
14k
It's Worth the Effort
3n
183
28k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
17
2.3k
Transcript
AndroidXͱKotlin Corou.nes @takuji31 ؼ͖ͬͯͨؔϞόΠϧΞϓϦݚڀձ
ࣗݾհ
ࣗݾհ • @takuji31 id:takuji31 • Takuji Nishibayashi • Hatena Co.,
Ltd. • App Team • ίϛοΫDAYS • δϟϯϓϧʔΩʔʂ • ΧΫϤϜ
ࣗݾհ • Android • Kotlin • Flu-er • ! h-ps:/
/blog.takuji31.jp • " h-ps:/ /nazuna.takuji31.jp • # $ h-ps:/ /photo.takuji31.jp
AndroidX
LiveData? ViewModel? AppCompat? Naviga5on
ͬͯ·͢ΑͶ
Kotlin Corou+nes
ͬͯ·͔͢ʁ
Google I/O 2019
Android development will become increasingly Kotlin-first — h$ps://android-developers.googleblog.com/2019/05/google- io-2019-empowering-developers-to-build-experiences-on-Android- Play.html
AndroidXʹKotlinαϙʔτ͕ଓʑ
Ұ෦ → h$ps:/ /speakerdeck.com/ takuji31/androidxniqian-mubian-li- nayatura
ࠓCorou%neαϙʔτͷΛ͠ ·͢
α൛ͷͷؚ·ΕΔͷͰ ͝ར༻ܭըతʹ
ViewModel.viewModelScope
ViewModelͷonClearedͰΩϟϯ ηϧ͞ΕΔCoroutineScope
ViewModelͰCorou,neΛ͍ ͘͢ͳΔ
Dispatcher Dispatchers.Main͕ઃఆ͞ΕΔ
໌ࣔతʹࢦఆ͠ͳ͍ݶΓશͯͷॲཧ ͕ϝΠϯεϨουͰ࣮ߦ
class UserViewModel : ViewModel() { val name: MutableLiveData<String> = MutableLiveData()
init { viewModelScope.launch { try { val user = withContext(Dispatchers.Default) { userRepository.fetchMyUser() } name.value = user.name } catch (e: IOException) { // error handling } } } }
class UserViewModel : ViewModel() { val name: MutableLiveData<String> = MutableLiveData()
init { viewModelScope.launch { try { val user = withContext(Dispatchers.Default) { userRepository.fetchMyUser() } name.value = user.name } catch (e: IOException) { // error handling } } } }
class UserViewModel : ViewModel() { val name: MutableLiveData<String> = MutableLiveData()
init { viewModelScope.launch { try { val user = withContext(Dispatchers.Default) { userRepository.fetchMyUser() } name.value = user.name } catch (e: IOException) { // error handling } } } }
class UserViewModel : ViewModel() { val name: MutableLiveData<String> = MutableLiveData()
init { viewModelScope.launch { try { val user = withContext(Dispatchers.Default) { userRepository.fetchMyUser() } name.value = user.name } catch (e: IOException) { // error handling } } } }
class UserViewModel : ViewModel() { val name: MutableLiveData<String> = MutableLiveData()
init { viewModelScope.launch { try { val user = withContext(Dispatchers.Default) { userRepository.fetchMyUser() } name.value = user.name } catch (e: IOException) { // error handling } } } }
class UserViewModel : ViewModel() { val name: MutableLiveData<String> = MutableLiveData()
init { viewModelScope.launch { try { val user = withContext(Dispatchers.Default) { userRepository.fetchMyUser() } name.value = user.name } catch (e: IOException) { // error handling } } } }
class UserViewModel : ViewModel() { val name: MutableLiveData<String> = MutableLiveData()
init { viewModelScope.launch { try { val user = withContext(Dispatchers.Default) { userRepository.fetchMyUser() } name.value = user.name } catch (e: IOException) { // error handling } } } }
class UserViewModel : ViewModel() { val name: MutableLiveData<String> = MutableLiveData()
init { viewModelScope.launch { try { val user = withContext(Dispatchers.Default) { userRepository.fetchMyUser() } name.value = user.name } catch (e: IOException) { // error handling } } } }
class UserViewModel : ViewModel() { val name: MutableLiveData<String> = MutableLiveData()
init { viewModelScope.launch { try { val user = withContext(Dispatchers.Default) { userRepository.fetchMyUser() } name.value = user.name } catch (e: IOException) { // error handling } } } }
※DispatcherΛઃఆ͠ͳ͍··ॏ͍ ॲཧΛ͠ͳ͍
LifecycleOwner .lifecycleScope
Lifecycleͱ࿈ಈͨ͠ CoroutineScope
Lifecycle.State.DESTROYED ʹͳͬͨ࣌ʹΩϟϯηϧ͞ΕΔ CoroutineScope
whenCreated / whenStarted / whenResumed
fun onCreate(savedInstanceState: Bundle?) { lifecycleScope.launch { whenResumed { val user
= withContext(Dispatchers.Default) { try { userRepository.fetchMyUser() } catch (e: IOException) { null } } if (user == null) { showErrorAndFinish() } viewModel.setup(user) } } }
fun onCreate(savedInstanceState: Bundle?) { lifecycleScope.launch { whenResumed { val user
= withContext(Dispatchers.Default) { try { userRepository.fetchMyUser() } catch (e: IOException) { null } } if (user == null) { showErrorAndFinish() } viewModel.setup(user) } } }
fun onCreate(savedInstanceState: Bundle?) { lifecycleScope.launch { whenResumed { val user
= withContext(Dispatchers.Default) { try { userRepository.fetchMyUser() } catch (e: IOException) { null } } if (user == null) { showErrorAndFinish() } viewModel.setup(user) } } }
fun onCreate(savedInstanceState: Bundle?) { lifecycleScope.launch { whenResumed { val user
= withContext(Dispatchers.Default) { try { userRepository.fetchMyUser() } catch (e: IOException) { null } } if (user == null) { showErrorAndFinish() } viewModel.setup(user) } } }
fun onCreate(savedInstanceState: Bundle?) { lifecycleScope.launch { whenResumed { val user
= withContext(Dispatchers.Default) { try { userRepository.fetchMyUser() } catch (e: IOException) { null } } if (user == null) { showErrorAndFinish() } viewModel.setup(user) } } }
liveData()
Corou%neΛLiveDataʹม
class UserViewModel : ViewModel() { val userName: LiveData<String> = liveData
{ val user = userRepository.fetchMyUser() emit(user.name) } }
class UserViewModel : ViewModel() { val userName: LiveData<String> = liveData
{ val user = userRepository.fetchMyUser() emit(user.name) } }
class UserViewModel : ViewModel() { val userName: LiveData<String> = liveData
{ val user = userRepository.fetchMyUser() emit(user.name) } }
class UserViewModel : ViewModel() { val userName: LiveData<String> = liveData
{ val user = userRepository.fetchMyUser() emit(user.name) } }
class UserViewModel : ViewModel() { val userName: LiveData<String> = liveData
{ val user = userRepository.fetchMyUser() emit(user.name) } }
Ҿͷblock͕suspended func.on
LiveData͕Ac*veʹͳͬͨ࣌ʹblock ͷॲཧ͕࣮ߦ͞ΕΔ
ྫ֎͕ൃੜͨ͠ΒͦΕҎ͕߱ྲྀ Εͳ͍
ͦͷଞ
ͦͷଞ • CoroutineWorker • Work ManagerͷWorkerͷdoWork͕suspended func2onͳόʔδϣϯ • όοΫάϥϯυͰඇಉظॲཧΛ͍ͨ࣌͠ʹ͑ͦ͏ •
room-ktx • RoomͷDAOͷϝιουʹsuspended func2on͕͑Δ • @Transac2onͱऍͨ͠ϝιουΛݺͿͱੜ͞ΕΔDAOͷϝιου ͕τϥϯβΫγϣϯͰ·ͱΊΒΕΔ
h"ps:/ /developer.android.com/ topic/libraries/architecture/ corou6nes
Enjoy Kotlin Corou.nes life!