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

Coroutines and Flow integration with Android Ar...

Paolo Rotolo
November 27, 2019

Coroutines and Flow integration with Android Architecture Components

In just two years, Andorid development has radically evolved: Google I/O 2017 marked the introduction of Kotlin as a supported language in the platform and just two years later, Google announced that Android Development will be Kotlin-first.

In this talk, we'll learn together what Coroutines are and how they're changing the way Android Developers are writing code. Then, we will explore the world of Architecture Components, learning how ViewModel, LiveData, WorkManager and Room can integrate with Kotlin language features like Coroutines and Flow, making the development of an app architecture easier than ever. We'll also have some `fun` converting old callbacks in the Andoird framework to Kotlin suspend functions!

Paolo Rotolo

November 27, 2019
Tweet

More Decks by Paolo Rotolo

Other Decks in Technology

Transcript

  1. Execute on a non-UI thread Get informed when the task

    is done Android solutions • AsyncTask • RxJava • Thread / HandlerThread / Executor with callbacks
  2. Execute on a non-UI thread Get informed when the task

    is done Android solutions • AsyncTask • RxJava • Thread / HandlerThread / Executor with callbacks
  3. Execute on a non-UI thread Get informed when the task

    is done Android solutions • AsyncTask • RxJava • Thread / HandlerThread / Executor with callbacks
  4. Execute on a non-UI thread Get informed when the task

    is done callbacks!!! • AsyncTask • RxJava • Thread / HandlerThread / Executor with Android solutions
  5. • AsyncTask • RxJava • Thread / HandlerThread / Executor

    with Execute on a non-UI thread Get informed when the task is done callbacks!!!1 Android solutions
  6. • AsyncTask • RxJava • Thread / HandlerThread / Executor

    with Execute on a non-UI thread Get informed when the task is done callbacks!!!1 Android solutions Deprecated this month!
  7. 11

  8. 12

  9. 13

  10. 14

  11. 15

  12. 16

  13. 17

  14. • AsyncTask • RxJava • Thread / HandlerThread / Executor

    with Execute on a non-UI thread Get informed when the task is done callbacks!!!1 Android solutions Deprecated this month!
  15. • AsyncTask • RxJava • Thread / HandlerThread / Executor

    with Execute on a non-UI thread Get informed when the task is done callbacks!!!1 Android solutions Deprecated this month! Need to handle cancellation
  16. Threads and callbacks fun login(name, pass) { val thread =

    Thread(Runnable() -> { requestLogin(name, pass) { result -> show(result) } } thread.start() }
  17. Threads and callbacks fun login(name, pass) { val thread =

    Thread(Runnable() -> { requestLogin(name, pass) { result -> Handler(Looper.getMainLooper()) { show(result) } } }
  18. onDraw onDraw onDraw requestLogin UI Thread fun login(name, pass) {

    requestLogin(name, pass) { result -> show(result) } }
  19. onDraw onDraw onDraw requestLogin UI Thread fun login(name, pass) {

    requestLogin(name, pass) { result -> show(result) } }
  20. onDraw onDraw onDraw requestLogin UI Thread show onDraw fun login(name,

    pass) { requestLogin(name, pass) { result -> show(result) } }
  21. onDraw onDraw onDraw requestLogin UI Thread show onDraw fun login(name,

    pass) { requestLogin(name, pass) { result -> show(result) } } onDraw onDraw onDraw
  22. When all of the coroutines on the main thread are

    suspended, the main thread is free to do other work.
  23. Dispatchers.Default • Common pool of shared background threads • Use

    it for: computing-intensive coroutines Dispatchers.Main • Main thread • Use it for: UI operations
  24. Dispatchers.Default • Common pool of shared background threads • Use

    it for: computing-intensive coroutines Dispatchers.IO • Shared pool of on-demand created threads • Use it for: IO-intensive blocking operations Dispatchers.Main • Main thread • Use it for: UI operations
  25. Dispatchers.Default • Common pool of shared background threads • Use

    it for: computing-intensive coroutines Dispatchers.IO • Shared pool of on-demand created threads • Use it for: IO-intensive blocking operations Dispatchers.Main • Main thread • Use it for: UI operations Common thread pools!
  26. Dispatchers.Default • Common pool of shared background threads • Use

    it for: computing-intensive coroutines Dispatchers.IO • Shared pool of on-demand created threads • Use it for: IO-intensive blocking operations Dispatchers.Main • Main thread • Use it for: UI operations Common thread pools! Easy to switch dispatchers!
  27. Login button pressed Launch coroutine requestLogin Show login status val

    job = Job() val uiScope = CoroutineScope(Dispatchers.Main + job)
  28. Login button pressed Launch coroutine requestLogin Show login status val

    job = Job() val uiScope = CoroutineScope(Dispatchers.Main + job) fun login(username: String, pass: String ) = uiScope.launch { val result = requestLogin(name, pass) show(result) }
  29. Login button pressed Launch coroutine requestLogin Show login status val

    job = Job() val uiScope = CoroutineScope(Dispatchers.Main + job) fun login(username: String, pass: String ) = uiScope.launch(Dispatchers.IO) { val result = requestLogin(name, pass) show(result) }
  30. 60

  31. Login button pressed Launch coroutine requestLogin Show login status ViewModel

    fun onCleared() { super.onCleared() job.cancel() }
  32. class MyViewModel: ViewModel() { val viewModelJob = Job() val uiScope

    = CoroutineScope(Dispatchers.Main+viewModelJob) fun login( username: String, pass: String ) = uiScope.launch(Dispatchers.IO) { val result = requestLogin(name, pass) show(result) } fun onCleared() { super.onCleared() viewModelJob.cancel() } }
  33. class MyViewModel: ViewModel() { val viewModelJob = Job() val uiScope

    = CoroutineScope(Dispatchers.Main+viewModelJob) fun login( username: String, pass: String ) = uiScope.launch(Dispatchers.IO) { val result = requestLogin(name, pass) show(result) } fun onCleared() { super.onCleared() viewModelJob.cancel() } } lifecycle-viewmodel-ktx
  34. class MyViewModel: ViewModel() { fun login( username: String, pass: String

    ) = viewModelScope.launch(Dispatchers.IO) { val result = requestLogin(name, pass) show(result) } }
  35. class MyViewModel: ViewModel() { fun login( username: String, pass: String

    ) = viewModelScope.launch(Dispatchers.IO) { val result = requestLogin(name, pass) show(result) } }