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
73
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
生成AIをより賢く エンジニアのための RAG入門 - Oracle AI Jam Session #20
kutsushitaneko
4
220
Qiita埋め込み用スライド
naoki_0531
0
860
ずっと昔に Star をつけたはずの思い出せない GitHub リポジトリを見つけたい!
rokuosan
0
150
re:Invent 2024 Innovation Talks(NET201)で語られた大切なこと
shotashiratori
0
300
watsonx.ai Dojo #5 ファインチューニングとInstructLAB
oniak3ibm
PRO
0
160
生成AIのガバナンスの全体像と現実解
fnifni
1
180
新機能VPCリソースエンドポイント機能検証から得られた考察
duelist2020jp
0
210
DevOps視点でAWS re:invent2024の新サービス・アプデを振り返ってみた
oshanqq
0
180
複雑性の高いオブジェクト編集に向き合う: プラガブルなReactフォーム設計
righttouch
PRO
0
110
NW-JAWS #14 re:Invent 2024(予選落ち含)で 発表された推しアップデートについて
nagisa53
0
250
KnowledgeBaseDocuments APIでベクトルインデックス管理を自動化する
iidaxs
1
260
ゼロから創る横断SREチーム 挑戦と進化の軌跡
rvirus0817
2
260
Featured
See All Featured
KATA
mclloyd
29
14k
We Have a Design System, Now What?
morganepeng
51
7.3k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Site-Speed That Sticks
csswizardry
2
190
[RailsConf 2023] Rails as a piece of cake
palkan
53
5k
It's Worth the Effort
3n
183
28k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
How to Think Like a Performance Engineer
csswizardry
22
1.2k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
The Cult of Friendly URLs
andyhume
78
6.1k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
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!