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
330
AndroidXとKotlin Coroutines
Takuji Nishibayashi
May 15, 2019
Tweet
Share
More Decks by Takuji Nishibayashi
See All by Takuji Nishibayashi
CameraX使ってみた
takuji31
0
66
kotlinx.datetime 使ってみた
takuji31
0
260
HiltのCustom Componentについて
takuji31
0
150
java.timeをAndroidで使う
takuji31
0
60
KSPを使ってコード生成
takuji31
0
290
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
960
kotlinx.serialization
takuji31
0
540
kanmoba-returns-02.pdf
takuji31
0
180
AndroidXに潜む便利なヤツら
takuji31
0
160
Other Decks in Technology
See All in Technology
いざ、BSC討伐の旅
nikinusu
2
780
SRE×AIOpsを始めよう!GuardDutyによるお手軽脅威検出
amixedcolor
0
170
[CV勉強会@関東 ECCV2024 読み会] オンラインマッピング x トラッキング MapTracker: Tracking with Strided Memory Fusion for Consistent Vector HD Mapping (Chen+, ECCV24)
abemii
0
220
SREが投資するAIOps ~ペアーズにおけるLLM for Developerへの取り組み~
takumiogawa
1
420
『Firebase Dynamic Links終了に備える』 FlutterアプリでのAdjust導入とDeeplink最適化
techiro
0
130
Application Development WG Intro at AppDeveloperCon
salaboy
0
190
iOSチームとAndroidチームでブランチ運用が違ったので整理してます
sansantech
PRO
0
150
TypeScript、上達の瞬間
sadnessojisan
46
13k
日経電子版のStoreKit2フルリニューアル
shimastripe
1
140
DynamoDB でスロットリングが発生したとき_大盛りver/when_throttling_occurs_in_dynamodb_long
emiki
1
430
プロダクト活用度で見えた真実 ホリゾンタルSaaSでの顧客解像度の高め方
tadaken3
0
180
Oracle Cloud Infrastructureデータベース・クラウド:各バージョンのサポート期間
oracle4engineer
PRO
28
13k
Featured
See All Featured
Building Adaptive Systems
keathley
38
2.3k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Thoughts on Productivity
jonyablonski
67
4.3k
Designing Experiences People Love
moore
138
23k
Why Our Code Smells
bkeepers
PRO
334
57k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
The Language of Interfaces
destraynor
154
24k
Visualization
eitanlees
145
15k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Teambox: Starting and Learning
jrom
133
8.8k
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!