$30 off During Our Annual Pro Sale. View Details »

A gentle introduction to Kotlin Coroutines

A gentle introduction to Kotlin Coroutines

Arturo Mejia

March 25, 2019
Tweet

More Decks by Arturo Mejia

Other Decks in Programming

Transcript

  1. A gentle introduction
    to Kotlin Coroutines
    Arturo Mejia

    Twitter: @ArturoMejia481

    ArturoMejiaMarmol.com

    View Slide

  2. View Slide

  3. Alternatives
    @ArturoMejia481
    Threads

    Callbacks

    RX (Reactive extensions)

    Futures

    Promises

    View Slide

  4. Threads
    @ArturoMejia481
    Expensive, not easy to use.
    IOS deprecate threads. DispatchQueue

    View Slide

  5. Callbacks
    @ArturoMejia481
    Don't call me, I'll call you!

    View Slide

  6. Callbacks
    @ArturoMejia481
    Callback Hell

    View Slide

  7. Callbacks
    @ArturoMejia481
    Callback Hell a.k.a Tilted Christmas Tree.

    View Slide

  8. Callbacks
    @ArturoMejia481
    Difficult error handling.

    View Slide

  9. Promises and Futures
    @ArturoMejia481

    View Slide

  10. Reactive Extensions
    @ArturoMejia481
    You have to change the way that you approach a problem.
    Operators
    RX
    Schedulers
    Streams

    View Slide

  11. @ArturoMejia481
    Reactive Extensions

    View Slide

  12. @ArturoMejia481
    Kotlin Coroutines
    Do async work the same way,

    exceptions, loops, etc…

    View Slide

  13. @ArturoMejia481
    Kotlin Coroutines
    Clear differences between suspend functions

    View Slide

  14. @ArturoMejia481
    Kotlin Coroutines
    Same function signature

    View Slide

  15. Coroutines = Lightweight
    @ArturoMejia481
    Threads
    Coroutines

    View Slide

  16. @ArturoMejia481
    Kotlin Coroutines

    View Slide

  17. Goal
    @ArturoMejia481
    Suspend

    Dispatchers

    Coroutine Builders

    Launch

    Async/await

    View Slide

  18. Suspend
    @ArturoMejia481
    A key word to indicate the compiler that a
    function can be suspended.

    View Slide

  19. Suspend
    @ArturoMejia481
    A suspend function should be called from a
    coroutine or another suspend function.

    View Slide

  20. Suspend
    @ArturoMejia481
    The Kotlin compiler transforms the function to a
    callback model and uses state machines to know
    when a function should be suspended or resume.

    View Slide

  21. @ArturoMejia481
    Coroutine ≈ Threads

    View Slide

  22. Let’s get our hands dirty!
    @ArturoMejia481

    View Slide

  23. Launch
    @ArturoMejia481
    Launch is when we want to fire and forget.

    View Slide

  24. Async
    @ArturoMejia481
    Async is when we want to run something, then
    await the result.

    View Slide

  25. Dispatchers
    @ArturoMejia481
    DEFAULT (Computational) Uses the # cores of the CPU, not
    good for computations like I/O, network, disk, etc.

    IO designed for doing blocking I/O operations, max #64
    thread, share the pool with DEFAULT.

    MAIN designed to operate UI objects.
    Indicate in which thread pool a coroutine should run

    View Slide

  26. Interoperate
    @ArturoMejia481
    kotlinx-coroutines-rx2: Utilities for Rx.

    kotlinx-coroutines-jdk8: Integration with JDK8
    CompletableFuture (Android API level 24).

    kotlinx-coroutines-android: Designed to operate UI objects.

    View Slide

  27. “With a great power, comes
    responsibility”
    @ArturoMejia481
    Spiderman

    View Slide

  28. Questions?
    @ArturoMejia481

    View Slide

  29. Thanks!
    Twitter: @ArturoMejia481
    ArturoMejiaMarmol.com
    Android Tutorials
    RayWenderLich.com/u/amejia481

    View Slide