AndroidXとKotlin Coroutines

AndroidXとKotlin Coroutines

E28404a128b6014e6f499d2a967c72a0?s=128

Takuji Nishibayashi

May 15, 2019
Tweet

Transcript

  1. 3.

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

    Ltd. • App Team • ίϛοΫDAYS • δϟϯϓϧʔΩʔʂ • ΧΫϤϜ
  2. 4.

    ࣗݾ঺հ • Android • Kotlin • Flu-er • ! h-ps:/

    /blog.takuji31.jp • " h-ps:/ /nazuna.takuji31.jp • # $ h-ps:/ /photo.takuji31.jp
  3. 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 } } } }
  4. 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 } } } }
  5. 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 } } } }
  6. 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 } } } }
  7. 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 } } } }
  8. 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 } } } }
  9. 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 } } } }
  10. 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 } } } }
  11. 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 } } } }
  12. 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) } } }
  13. 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) } } }
  14. 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) } } }
  15. 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) } } }
  16. 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) } } }
  17. 42.

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

    { val user = userRepository.fetchMyUser() emit(user.name) } }
  18. 43.

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

    { val user = userRepository.fetchMyUser() emit(user.name) } }
  19. 44.

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

    { val user = userRepository.fetchMyUser() emit(user.name) } }
  20. 45.

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

    { val user = userRepository.fetchMyUser() emit(user.name) } }
  21. 46.

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

    { val user = userRepository.fetchMyUser() emit(user.name) } }
  22. 50.
  23. 51.

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

    room-ktx • RoomͷDAOͷϝιουʹsuspended func2on͕࢖͑Δ • @Transac2onͱ஫ऍͨ͠ϝιουΛݺͿͱੜ੒͞ΕΔDAOͷϝιου ͕τϥϯβΫγϣϯͰ·ͱΊΒΕΔ