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
64
kotlinx.datetime 使ってみた
takuji31
0
260
HiltのCustom Componentについて
takuji31
0
150
java.timeをAndroidで使う
takuji31
0
59
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
OCI 運用監視サービス 概要
oracle4engineer
PRO
0
4.8k
EventHub Startup CTO of the year 2024 ピッチ資料
eventhub
0
110
データプロダクトの定義からはじめる、データコントラクト駆動なデータ基盤
chanyou0311
2
300
誰も全体を知らない ~ ロールの垣根を超えて引き上げる開発生産性 / Boosting Development Productivity Across Roles
kakehashi
1
220
ドメイン名の終活について - JPAAWG 7th -
mikit
33
20k
Incident Response Practices: Waroom's Features and Future Challenges
rrreeeyyy
0
160
Application Development WG Intro at AppDeveloperCon
salaboy
0
180
Python(PYNQ)がテーマのAMD主催のFPGAコンテストに参加してきた
iotengineer22
0
470
iOSチームとAndroidチームでブランチ運用が違ったので整理してます
sansantech
PRO
0
130
エンジニア人生の拡張性を高める 「探索型キャリア設計」の提案
tenshoku_draft
1
120
SSMRunbook作成の勘所_20241120
koichiotomo
2
130
OCI Security サービス 概要
oracle4engineer
PRO
0
6.5k
Featured
See All Featured
Music & Morning Musume
bryan
46
6.2k
Fireside Chat
paigeccino
34
3k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Optimizing for Happiness
mojombo
376
70k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
GraphQLとの向き合い方2022年版
quramy
43
13k
GitHub's CSS Performance
jonrohan
1030
460k
Automating Front-end Workflow
addyosmani
1366
200k
KATA
mclloyd
29
14k
Gamification - CAS2011
davidbonilla
80
5k
Typedesign – Prime Four
hannesfritz
40
2.4k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.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!