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

JetpackのKotlin Coroutineサポート

JetpackのKotlin Coroutineサポート

739aa00e2fb60cc14a21b3a741d9d9a2?s=128

SotaHagiwara

May 19, 2019
Tweet

Transcript

  1. Jetpack Kotlin Coroutine Souta Hagiwara 2019/05/19 I/O Extended 2019 Tokyo

    @GDG
  2. Twitter => @___soprog ( 3 ) GitHub => @souta0104 Android

    1 Kotlin 10 Souta Hagiwara
  3. Coroutine

  4. Coroutine

  5. class MainViewModel( private val repository: MainRepository ) : ViewModel(), CoroutineScope

    { private val job = Job() override val coroutineContext: CoroutineContext get() = job + Dispatchers.IO fun addTask(task: Task) { launch { repository.add(task) } } override fun onCleared() { job.cancel() super.onCleared() } }
  6. CoroutineScope Coroutine class MainViewModel( private val repository: MainRepository ) :

    ViewModel(), CoroutineScope { private val job = Job() override val coroutineContext: CoroutineContext get() = job + Dispatchers.IO fun addTask(task: Task) { launch { repository.add(task) } } override fun onCleared() { job.cancel() super.onCleared() } }
  7. class MainViewModel( private val repository: MainRepository ) : ViewModel(), CoroutineScope

    { private val job = Job() override val coroutineContext: CoroutineContext get() = job + Dispatchers.IO fun addTask(task: Task) { launch { repository.add(task) } } override fun onCleared() { job.cancel() super.onCleared() } } Job coroutineContext coroutineContext.cancel() job onCleared()
  8. class MainViewModel( private val repository: MainRepository ) : ViewModel(), CoroutineScope

    { private val job = Job() override val coroutineContext: CoroutineContext get() = job + Dispatchers.IO fun addTask(task: Task) { launch { repository.add(task) } } override fun onCleared() { job.cancel() super.onCleared() } } launch withContext CoroutineBuilder
  9. Coroutine

  10. Android

  11. Thread AsyncTask

  12. None
  13. RxJava LiveData Coroutine

  14. None
  15. Coroutine ..?

  16. Coroutine

  17. Coroutine

  18. ViewModelScope def lifecycle_version = "2.2.0-alpha01" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" class MainViewModel( private

    val repository: MainRepository ) : ViewModel() { fun addTask(task: Task) { viewModelScope.launch(Dispatchers.IO) { /* blocking task */ } } } job cancel Dispatcher Main api IO
  19. LifecycleScope def lifecycle_version = "2.2.0-alpha01" implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version" Lifecycle.State.DESTROYED State STARTED

    (launchWhenStarted) CREATED (launchWhenCreated) RESUMED (launchWhenResumed) lifecycleScope.launch { /* Blocking Task */ } lifecycleScope.launchWhenStarted { /* Blocking Task */ } Activity, Fragment
  20. liveData() def lifecycle_version = "2.2.0-alpha01" implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" liveData{} emit() LiveData

    Active emit withContext livedata post Api DB Coroutine LiveData fun loadTask() = liveData<Task> { emit(database.load()) val task = api.fetch() database.insert(task) emit(database.load()) } @Query(...) suspend fun load(): Task Database.kt (Room) Repository.kt inactive active
  21. liveData() def lifecycle_version = "2.2.0-alpha01" implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" LiveData<T> emitSource() fun

    loadTask() = liveData<Task> { emitSource(database.loadWithLiveData()) val task = api.fetch() database.insert(task) } @Query(...) fun loadWithLiveData(): LiveData<Task> Database.kt (Room) Repository.kt Room LiveData<T> emitSource()
  22. Room DAO suspend function CoroutineWorker !!!! !!!!!! !!!!!

  23. Retro t suspend function

  24. Coroutine First LiveData ( )

  25. Thanks