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

256
Views
Kotlin: define a coroutine scope in App class

Is it a good practice to declare a coroutine scope inside the App class instead of using a Globascope?

class App : Application() {

    val applicationJob = Job()

    companion object {
        lateinit var instance: App
            private set

        lateinit var appDefaultScope: CoroutineScope
    }

    override fun onCreate() {
        super.onCreate()
        instance = this@App
        appDefaultScope = CoroutineScope(Dispatchers.Default + applicationJob)
    }

}

Here I defined a variable appDefaultScope and initialized it in the App's onCreate

So anywhere in my code I could always do:

App.appDefaultScope.launch {
   // some operations
}

The main difference with a GlobalScope that I see is that here I can always cancel the job and shut down all potentially stuck coroutines.

Are there are better alternatives to this?

The reason is that i have some object functions that use coroutines and are not called from an activity (then I cannot provide them with a scope defined inside the activity) but need a scope for their operations. An example of such object function is a Log that writes on a local database that can be called almost everywhere in the App.

over 4 years ago · Santiago Trujillo
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!