Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
Basic coroutine network call in a library

I'm just reading and learning about coroutines in Kotlin to use in a small library for fun/learning purposes. In the documentation, you can do something like

GlobalScope.launch {

}

So in my method,

fun myMethod() {
    GlobalScope.launch {
        // do some networking code
    }   
}

Is it best practice to use GlobalScope to launch a coroutine for a library? In the docs it says

Application code usually should use application-defined CoroutineScope, using async or launch on the instance of GlobalScope is highly discouraged.

This is obviously a library and not necessarily app code. But I wasn't sure if that was the best way to do networking in the background with coroutines.

I also tried

runBlocking {
    async {
        // do some networking code
    }
}

Thinking that runBlocking introduces a new coroutine scope, but I think in this case it inherits the scope from its parents which is the main thread so then I get an exception about no networking on the main UI thread.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can create a suspending function, and this will enforce that the user calls this from a coroutine themselves. If your method uses withContext you don't have to worry about GlobalScope or configuring the scope. withContext simply tells the coroutine which context to use (for networking, you'll want IO. And the scope is determined now by however the user launches it.

The way I'd construct your method would be:

suspend fun myMethod() = withContext(Dispatchers.IO) {
    // do some networking code
}
over 4 years ago · Santiago Trujillo Report

0

Depending on what you are trying to achieve, you can use different patterns even inside a library.

If your function is supposed to immediately return after launching some coroutines, the "best practice" is to declare your function as an extension of the CoroutineScope, that way you don't have to use the global scope:

fun CoroutineScope.launchesAndReturnsImmediately() {
    // launch can be called because we are extending CoroutineScope
    launch {
        // some work
    }
}

That being said, you often don't need to return immediately when in a library, so you can declare your functions suspend instead, which is easier to grasp on the consumer side. Multiple options there:

  • do some work in the background by using withContext to run on appropriate thread pool, or call other suspending functions
  • use coroutineScope to launch child coroutines and decompose the work, but still suspend until all child coroutines are done
  • "create" a suspension by wrapping callback-based asynchronous code, using suspendCoroutine or suspendCancellableCoroutine
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!