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

A Practical Introduction to Coroutines

A Practical Introduction to Coroutines

Kotlin coroutines are stable now, and they're starting to gain adoption. If you're new to coroutines, or if you've never used them, this talk is for you. We'll cover:

* What coroutines are
* What they can do, and where you'll want to use them
* Current limitations
* A practical example of using coroutines

You can find the companion code here:
https://gitlab.com/fuentespatrick/a-practical-introduction-to-coroutines-talk-companion/

Patrick Fuentes

January 26, 2019
Tweet

Other Decks in Technology

Transcript

  1. Avoiding Callback Hell fun loadFriendOrder() { repository.getUser( { user ->

    repository.getUserContactIds(user, { contactIdList -> val userToDisplay = findUserToDisplay(contactIdList) repository.getOrderHistory(userToDisplay, { orderIdList -> findOrderToDisplay(orderIdList) }, { throwable -> Log.e(TAG, "${throwable.message} - ${throwable.cause}") }) }, { throwable -> Log.e(TAG, "${throwable.message} - ${throwable.cause}") }) }, { throwable -> Log.e(TAG, "${throwable.message} - ${throwable.cause}") }) } suspend fun loadFriendOrder() { try { val user = repository.getUser() val contactIdList = repository.getUserContactIds(user) val userToDisplay = findUserToDisplay(contactIdList) val orderIdList = repository.getOrderHistory(userToDisplay) findOrderToDisplay(orderIdList) } catch (error: Throwable) { Log.e(TAG, "${error.message} - ${error.cause}") } }
  2. Job

  3. Dispatchers – Where your code runs Default – The default

    if you don’t specify. Backed by a shared thread pool IO – Best for IO-intensive blocking operations (like file I/O and blocking socket I/O) Main – A special CoroutineDispatcher: confined to the application “Main” or “UI” thread & used for any UI-based activities – usually single threaded Unconfined – Use with extreme caution, not for general code.
  4. Thread A Thread B Coroutine 1 Coroutine 8 Coroutine 4

    Coroutine 3 Coroutine 2 Coroutine 7 Coroutine 5
  5. Dispatchers – etc… Private thread pools can be created with

    newSingleThreadContext() and newFixedThreadPoolContext()
  6. Parent Jobs & Scope launch {…} Launch Context Parent Job

    async {…} Launch Scope Async Context Child Job Async Scope
  7. Resources • Read great resources: ◦ Understanding blocking vs suspending:

    https://medium.com/@elizarov/blocking-threads-suspending-coroutines-d33e11bf4761 ◦ Networking with coroutines: https://medium.com/exploring-android/android-networking-with-coroutines-and-retrofit-a2f2 0dd40a83 ◦ Demystifying CoroutineContext: https://proandroiddev.com/demystifying-coroutinecontext-1ce5b68407ad ◦ The official documentation is great: https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/coroutine-context-and-dispatc hers.md ◦ Coroutines Cheatsheet: https://blog.kotlin-academy.com/kotlin-coroutines-cheat-sheet-8cf1e284dc35 ◦ Any KotlinConf talks on Coroutines - especially ‘Coroutines in Practice’ by Roman Elizarov (*AFTER* you understand what we covered in this talk) ◦ Any talks or articles by Roman Elizarov – @relizarov
  8. What’s next? • Use coroutines this week – getting started

    is easy • Read the recommended resources from the last slide • Check out the project with my samples from this talk: https://gitlab.com/fuentespatrick/a-practical-introduction-to-coroutines-talk- companion/ • After you’ve tried the basics of coroutines, learn more ◦ Channels ◦ Select • Feel free to ask me questions on Twitter – @pfue
  9. Thank you! • All of you for being here! •

    St. Louis DevFest organizers for having me • Helping with the talk: ◦ Renee Vandervelde – @RenVandervelde ◦ Dan Lew – @danlew42 ◦ Aaron Weaver ◦ Brendon Justin – @brendonjustin_ ◦ Nate Brunette – @natebrunette