Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AndroidXとKotlin Coroutines

AndroidXとKotlin Coroutines

E28404a128b6014e6f499d2a967c72a0?s=128

Takuji Nishibayashi

May 15, 2019
Tweet

Transcript

  1. AndroidXͱKotlin Corou.nes @takuji31 ؼ͖ͬͯͨؔ੢ϞόΠϧΞϓϦݚڀձ

  2. ࣗݾ঺հ

  3. ࣗݾ঺հ • @takuji31 id:takuji31 • Takuji Nishibayashi • Hatena Co.,

    Ltd. • App Team • ίϛοΫDAYS • δϟϯϓϧʔΩʔʂ • ΧΫϤϜ
  4. ࣗݾ঺հ • Android • Kotlin • Flu-er • ! h-ps:/

    /blog.takuji31.jp • " h-ps:/ /nazuna.takuji31.jp • # $ h-ps:/ /photo.takuji31.jp
  5. AndroidX

  6. LiveData? ViewModel? AppCompat? Naviga5on

  7. ࢖ͬͯ·͢ΑͶ

  8. Kotlin Corou+nes

  9. ࢖ͬͯ·͔͢ʁ

  10. Google I/O 2019

  11. 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

  12. AndroidXʹ΋Kotlinαϙʔτ͕ଓʑ

  13. Ұ෦ → h$ps:/ /speakerdeck.com/ takuji31/androidxniqian-mubian-li- nayatura

  14. ࠓ೔͸Corou%neαϙʔτͷ࿩Λ͠ ·͢

  15. α൛ͷ΋ͷ΋ؚ·ΕΔͷͰ ͝ར༻͸ܭըతʹ

  16. ViewModel.viewModelScope

  17. ViewModelͷonClearedͰΩϟϯ ηϧ͞ΕΔCoroutineScope

  18. ViewModel಺ͰCorou,neΛ࢖͍΍ ͘͢ͳΔ

  19. Dispatcher͸ Dispatchers.Main͕ઃఆ͞ΕΔ

  20. ໌ࣔతʹࢦఆ͠ͳ͍ݶΓશͯͷॲཧ ͕ϝΠϯεϨουͰ࣮ߦ

  21. 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 } } } }
  22. 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 } } } }
  23. 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 } } } }
  24. 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 } } } }
  25. 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 } } } }
  26. 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 } } } }
  27. 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 } } } }
  28. 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 } } } }
  29. 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 } } } }
  30. ※DispatcherΛઃఆ͠ͳ͍··ॏ͍ ॲཧΛ͠ͳ͍

  31. LifecycleOwner .lifecycleScope

  32. Lifecycleͱ࿈ಈͨ͠ CoroutineScope

  33. Lifecycle.State.DESTROYED ʹͳͬͨ࣌ʹΩϟϯηϧ͞ΕΔ CoroutineScope

  34. whenCreated / whenStarted / whenResumed

  35. 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) } } }
  36. 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) } } }
  37. 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) } } }
  38. 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) } } }
  39. 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) } } }
  40. liveData()

  41. Corou%neΛLiveDataʹม׵

  42. class UserViewModel : ViewModel() { val userName: LiveData<String> = liveData

    { val user = userRepository.fetchMyUser() emit(user.name) } }
  43. class UserViewModel : ViewModel() { val userName: LiveData<String> = liveData

    { val user = userRepository.fetchMyUser() emit(user.name) } }
  44. class UserViewModel : ViewModel() { val userName: LiveData<String> = liveData

    { val user = userRepository.fetchMyUser() emit(user.name) } }
  45. class UserViewModel : ViewModel() { val userName: LiveData<String> = liveData

    { val user = userRepository.fetchMyUser() emit(user.name) } }
  46. class UserViewModel : ViewModel() { val userName: LiveData<String> = liveData

    { val user = userRepository.fetchMyUser() emit(user.name) } }
  47. Ҿ਺ͷblock͕suspended func.on

  48. LiveData͕Ac*veʹͳͬͨ࣌ʹblock ͷॲཧ͕࣮ߦ͞ΕΔ

  49. ྫ֎͕ൃੜͨ͠ΒͦΕҎ߱͸஋͕ྲྀ Εͳ͍

  50. ͦͷଞ

  51. ͦͷଞ • CoroutineWorker • Work ManagerͷWorkerͷdoWork͕suspended func2onͳόʔδϣϯ • όοΫάϥ΢ϯυͰඇಉظॲཧΛ͍ͨ࣌͠ʹ࢖͑ͦ͏ •

    room-ktx • RoomͷDAOͷϝιουʹsuspended func2on͕࢖͑Δ • @Transac2onͱ஫ऍͨ͠ϝιουΛݺͿͱੜ੒͞ΕΔDAOͷϝιου ͕τϥϯβΫγϣϯͰ·ͱΊΒΕΔ
  52. h"ps:/ /developer.android.com/ topic/libraries/architecture/ corou6nes

  53. Enjoy Kotlin Corou.nes life!