Giter Club home page Giter Club logo

coroutine-recipes's Introduction

Learn Kotlin Coroutines for Android by example

Article: Android Coroutine Recipes

Slides: Android Coroutine Recipes

Contains following examples:

  • How to launch a coroutine
  • How to launch coroutine with a timeout
  • How to launch coroutine which perform 2 background tasks sequentially
  • How to launch coroutine which perform 2 background tasks in parallel
  • How to cancel a coroutine
  • How to catch exception thrown inside coroutine (try/catch)
  • How to catch exception thrown inside coroutine (exception handler)

How to launch a coroutine

fun loadData() = launch(uiContext, parent = job) {
    showLoading() // ui thread

    val result = dataProvider.loadData() // non ui thread, suspend until finished

    showText(result) // ui thread
    hideLoading() // ui thread
}

You can get full code here

How to launch coroutine with a timeout

fun loadData() = launch(uiContext, parent = job) {
    showLoading()

    val result = withTimeoutOrNull(1, TimeUnit.SECONDS) { dataProvider.loadData() }

    showText(result ?: "Timeout")
    hideLoading()
}

You can get full code here

How to launch coroutine which perform 2 background tasks sequentially

fun loadData() = launch(uiContext, parent = job) {
    showLoading()

    val result1 = dataProvider.loadData()
    val result2 = dataProvider.loadData()

    showText("$result1\n$result2")
    hideLoading()
}

You can get full code here

How to launch coroutine which perform 2 background tasks in parallel

fun loadData() = launch(uiContext, parent = job) {
    showLoading()

    val result1 = async { dataProvider.loadData() }
    val result2 = async { dataProvider.loadData() }

    val data = "${result1.await()}\n${result2.await()}"
    showText(data)
    hideLoading()
}

You can get full code here

How to cancel a coroutine

private var job: Job = Job()

fun loadData() = launch(uiContext, parent = job) {
    ...
}

override fun onDestroyView() {
    super.onDestroyView()
    job.cancel()
}

You can get full code here

How to catch exception thrown inside coroutine (try/catch) source

fun loadData() = launch(uiContext, parent = job) {
    showLoading()

    try {
        val result = dataProvider.loadData()
        showText(result)
    } catch (e: IllegalArgumentException) {
        showText(e.message ?: "")
    }

    hideLoading()
}

You can get full code here

How to catch exception thrown inside coroutine (exception handler)

val exceptionHandler: CoroutineContext = CoroutineExceptionHandler { _, throwable ->
    showText(throwable.message ?: "")
    hideLoading()
}

// we can attach CoroutineExceptionHandler to parent context
fun loadData() = launch(uiContext + exceptionHandler, parent = job) {
    ...
}

You can get full code here

coroutine-recipes's People

Contributors

codeteo avatar ddanylyk avatar dmytrodanylyk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.