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

AndroidXとKotlin Coroutines

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

AndroidXとKotlin Coroutines

Avatar for Takuji Nishibayashi

Takuji Nishibayashi

May 15, 2019
Tweet

More Decks by Takuji Nishibayashi

Other Decks in Technology

Transcript

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

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

    /blog.takuji31.jp • " h-ps:/ /nazuna.takuji31.jp • # $ h-ps:/ /photo.takuji31.jp
  3. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. class UserViewModel : ViewModel() { val userName: LiveData<String> = liveData

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

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

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

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

    { val user = userRepository.fetchMyUser() emit(user.name) } }
  22. ͦͷଞ • CoroutineWorker • Work ManagerͷWorkerͷdoWork͕suspended func2onͳόʔδϣϯ • όοΫάϥ΢ϯυͰඇಉظॲཧΛ͍ͨ࣌͠ʹ࢖͑ͦ͏ •

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