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

Android Study Jams - Coroutine

GDG Montreal
September 29, 2021

Android Study Jams - Coroutine

This is a little introduction of Coroutine and the study jam link to go with it.

GDG Montreal

September 29, 2021
Tweet

More Decks by GDG Montreal

Other Decks in Programming

Transcript

  1. This work is licensed under the Apache 2.0 License
    Android Study Jams
    Modern Android Development

    View Slide

  2. This work is licensed under the Apache 2.0 License
    Modern Android Development
    Pre-requisites
    Coroutines
    MotionLayout
    Using Hilt in your Android App
    Learn the more advanced techniques of building Android apps using libraries, testing,
    Jetpack and more to increase the quality of your app
    ● Finished the First Two Tracks
    Curriculum used
    Advanced WorkManager
    Advanced Testing : Survey of Testing Topics
    Jetpack Compose Basics

    View Slide

  3. This work is licensed under the Apache 2.0 License
    What will you learn?
    2
    3
    1 Coroutines
    MotionLayout
    (1 hour)
    (1 hour)
    Coroutines are a Kotlin feature that converts async callbacks
    for long-running tasks, such as database or network access,
    into sequential code.
    MotionLayout is a library that lets you add rich
    motion into your Android app.
    Modern Android Development
    Using Hilt in your
    Android Apps
    (1 hour)
    Learn the importance of dependency injection (DI) to
    create a solid and extensible application that scales to
    large projects. We'll use Hilt as the DI tool to manage
    dependencies.

    View Slide

  4. This work is licensed under the Apache 2.0 License
    5 Advanced Testing:
    Survey of Topics
    Learn Coroutines, Room, Databinding and
    End-to-End Testing.
    6 Jetpack Compose
    Basics
    Learn Coroutines, Room, Databinding and
    End-to-End Testing.
    4 Advanced WorkManager (1 hour)
    Teaches advanced WorkManager concepts. It builds on
    the basic material covered in the Background Work with
    WorkManager codelab.
    What will you learn?
    Modern Android Development
    (2 hour)
    (1 hour)

    View Slide

  5. This work is licensed under the Apache 2.0 License
    Android Study Jams
    Modern Android Development
    Coroutines

    View Slide

  6. This work is licensed under the Apache 2.0 License
    TOPIC DATE
    Intro Coroutine Sept 22
    Return on Coroutine and intro to
    MotionLayout
    Sept 29
    Return on MotionLayout and intro
    Using Hilt in your Android Apps
    Oct 6
    Return on Hilt and intro to Advanced
    WorkManager
    Oct 13
    Return on Advanced WorkMananger
    and intro to Advanced Testing: Survey
    of Topics
    Oct 20
    Return on Advanced Testing and intro
    to Jetpack Compose Basics
    Oct 27
    Return on Jetpack Compose Basics
    and thank you
    Nov 3
    Android study Jam
    Schedule

    View Slide

  7. This work is licensed under the Apache 2.0 License
    Modern Android Development : Coroutines
    Android Study Jams

    View Slide

  8. This work is licensed under the Apache 2.0 License
    Learning Objectives
    ० Understand how to use coroutine.
    ० Use suspend functions to make async code sequential.
    ० Use launch and runBlocking to control how code executes.
    ० Learn techniques to convert existing APIs to coroutines
    using suspendCoroutine.
    ० Use coroutines with Architecture Components.
    ० Learn best practices for testing coroutines.

    View Slide

  9. This work is licensed under the Apache 2.0 License
    ० Familiarity with the Architecture Components
    ViewModel, LiveData, Repository, and Room.
    ० Experience with Kotlin syntax, including
    extension functions and lambdas.
    ० A basic understanding of using threads on
    Android, including the main thread, background
    threads, and callbacks.
    ० Android Studio 4.1
    Prerequisites

    View Slide

  10. This work is licensed under the Apache 2.0 License
    Concept Overview
    What is Coroutine?

    View Slide

  11. This work is licensed under the Apache 2.0 License
    The word “coroutine” is composed of two words: “co”
    (cooperative) and “routines” (functions).
    Coroutines are functions that are cooperative.
    Source : https://www.educative.io/edpresso/what-is-a-coroutine

    View Slide

  12. This work is licensed under the Apache 2.0 License
    How are functions
    non-cooperative?
    Source : https://www.educative.io/edpresso/what-is-a-coroutine
    Usually, when a function calls a second
    function, the first cannot continue until
    the second function finishes and returns
    to where it was called.

    View Slide

  13. This work is licensed under the Apache 2.0 License
    How can functions
    cooperate with one
    another?
    Coroutines are functions where the
    control is transferred from one
    function to the other function in a way
    that the exit point from the first
    function and the entry point to the
    second function are remembered –
    without changing the context.
    Source : https://www.educative.io/edpresso/what-is-a-coroutine

    View Slide

  14. This work is licensed under the Apache 2.0 License
    // Async callback
    networkRequest { result ->
    // Successful network request
    databaseSave(result) { row->
    }
    }
    // The same code with coroutine
    val result = networkRequest()
    // Successful network request
    databaseSave(result)
    // Result saved
    SEQUENTIAL
    ASYNC

    View Slide

  15. This work is licensed under the Apache 2.0 License
    ० Replace callbacks by sequential programming
    ० Ability to make your call main safe with suspend ( in Android )
    Advantage of Coroutines

    View Slide

  16. This work is licensed under the Apache 2.0 License
    Some basic tips

    View Slide

  17. This work is licensed under the Apache 2.0 License
    You can see suspend
    function in Android studio

    View Slide

  18. This work is licensed under the Apache 2.0 License
    “Exceptions in suspend functions work just like
    errors in regular functions. If you throw an error in a
    suspend function, it will be thrown to the caller.”

    View Slide

  19. This work is licensed under the Apache 2.0 License
    “The suspend keyword doesn't specify the thread
    code runs on. Suspend functions may run on a
    background thread or the main thread.”

    View Slide

  20. This work is licensed under the Apache 2.0 License
    “To switch between any dispatcher, coroutines uses
    withContext.”

    View Slide

  21. This work is licensed under the Apache 2.0 License
    “In Kotlin, all coroutines run inside a
    CoroutineScope”

    View Slide

  22. This work is licensed under the Apache 2.0 License
    What you’ll do
    ● Use suspend functions to make async code sequential
    ● Use launch and runBlocking to control how code executes.
    ● Convert APIs to coroutines using suspendCoroutine
    ● Best practices for testing

    View Slide

  23. This work is licensed under the Apache 2.0 License
    Let’s get started

    View Slide

  24. This work is licensed under the Apache 2.0 License
    Start here:
    https://developer.android.com/codelabs/kotlin-coroutines#0
    Collect your badges!

    View Slide

  25. This work is licensed under the Apache 2.0 License
    Carrie Sawyer
    Build Your First
    App Pathway
    Build an
    Interactive App
    Layouts
    Pathway
    Create a Developer Profile

    View Slide

  26. This work is licensed under the Apache 2.0 License
    Share what you’ve
    learned with
    #AndroidStudyJams

    View Slide

  27. This work is licensed under the Apache 2.0 License
    Have a Question? Just ask
    Join our slack : bit.ly/gdgmontrealslack
    Joins our channel : #android-study-jams-2021

    View Slide

  28. This work is licensed under the Apache 2.0 License
    Start here:
    https://developer.android.com/codelabs/kotlin-coroutines#0
    Collect your badges!

    View Slide

  29. This work is licensed under the Apache 2.0 License
    Welcome back
    And congrats!

    View Slide

  30. This work is licensed under the Apache 2.0 License
    Recap of Coroutines
    ० suspend
    ० Coroutine inside a
    viewModel
    ० Main Safe
    ० Cancel all
    coroutines in
    viewModel
    ० Coroutine with
    WorkManager
    ० Higher order
    function
    ० Moving callback to
    coroutine
    ० Testing

    View Slide

  31. This work is licensed under the Apache 2.0 License
    Learn More

    View Slide

  32. This work is licensed under the Apache 2.0 License
    Want to learn more about coroutine?
    ● Check out the " Advanced Coroutines with Kotlin Flow and LiveData"
    codelab to learn more advanced coroutines usage on Android.
    ● Cancellation and exceptions : Part 1: Coroutines, Part 2: Cancellation
    in coroutines, and Part 3: Exceptions in coroutines.
    ● Coroutine guides by JetBrains
    ● "Improve app performance with Kotlin coroutines" for more usage
    patterns of coroutines on Android.
    ● WorkManager
    Version 1.0

    View Slide

  33. This work is licensed under the Apache 2.0 License
    Want to learn more?
    ● Official Android Developers Site: https://developer.android.com/
    ● Android Samples on GitHub
    ● Official Android Developers Blog (for announcements)
    ● Android Developers Medium Blog (for more technical articles)
    ● Android Developers YouTube channel
    ● Follow @AndroidDev on Twitter
    ● Subscribe to the Android Developer Newsletter
    ● Official Kotlin language site
    Version 1.0

    View Slide