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
58
kotlinx.datetime 使ってみた
takuji31
0
220
HiltのCustom Componentについて
takuji31
0
140
java.timeをAndroidで使う
takuji31
0
53
KSPを使ってコード生成
takuji31
0
280
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
940
kotlinx.serialization
takuji31
0
530
kanmoba-returns-02.pdf
takuji31
0
170
AndroidXに潜む便利なヤツら
takuji31
0
150
Other Decks in Technology
See All in Technology
なんで、私がAWS Heroに!? 〜社外の広い世界に一歩踏み出そう〜
minorun365
PRO
6
1.1k
Amazon FSx for NetApp ONTAPを利用するにあたっての要件整理と設計のポイント
non97
1
160
ExaDB-D dbaascli で出来ること
oracle4engineer
PRO
0
3.6k
Apple/Google/Amazonの決済システムの違いを踏まえた定期購読課金システムの構築 / abema-billing-system
cyberagentdevelopers
PRO
1
220
独自ツール開発でスタジオ撮影をDX!「VLS(Virtual LED Studio)」 / dx-studio-vls
cyberagentdevelopers
PRO
1
180
Fargateを使った研修の話
takesection
0
120
プロダクト成長に対応するプラットフォーム戦略:Authleteによる共通認証基盤の移行事例 / Building an authentication platform using Authlete and AWS
kakehashi
1
150
신뢰할 수 있는 AI 검색 엔진을 만들기 위한 Liner의 여정
huffon
0
370
チームを主語にしてみる / Making "Team" the Subject
ar_tama
4
310
生成AIの強みと弱みを理解して、生成AIがもたらすパワーをプロダクトの価値へ繋げるために実践したこと / advance-ai-generating
cyberagentdevelopers
PRO
1
180
サイバーエージェントにおける生成AIのリスキリング施策の取り組み / cyber-ai-reskilling
cyberagentdevelopers
PRO
2
200
新卒1年目が挑む!生成AI × マルチエージェントで実現する次世代オンボーディング / operation-ai-onboarding
cyberagentdevelopers
PRO
1
170
Featured
See All Featured
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.2k
Intergalactic Javascript Robots from Outer Space
tanoku
268
27k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
355
29k
Statistics for Hackers
jakevdp
796
220k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Visualization
eitanlees
144
15k
Testing 201, or: Great Expectations
jmmastey
38
7k
Done Done
chrislema
181
16k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Into the Great Unknown - MozCon
thekraken
31
1.5k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
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!