title: Android Background Job Service on CoroutineScope and Dispatcher.IO
body: <h3>Context</h3>
  <p>I'm trying to update a couple of background Job in Android 6. The main goal is to make the work done by the background job light and non ui thread blocking. So I decided to some things:</p>
  <ul>
  <li>GlobalScope may be a bad idea since a dying thread will terminate the others. So CoroutineScope will to the trick</li>
  <li>Since I need I/O and Network stuff done, I need a thread pool designed for those tasks. So I'll use Dispatchers.IO</li>
  <li>The work of the bound JobServices are designed for &quot;fire and forget&quot;. The app doesn't care - so I'll just launch the job instead of waiting for the output of an async</li>
  </ul>
  <h3>Code</h3>
  <p>All together I designed the following JobService class as a template for others to be migrated or implemented. Since the job is shedulded the class will return false to the inherited functions.</p>
  <pre><code>import android.app.job.JobParameters
  import android.app.job.JobService
  import kotlinx.coroutines.CoroutineScope
  import kotlinx.coroutines.Dispatchers
  import kotlinx.coroutines.launch
  
  class TestJobService : JobService() {
  
      override fun onStartJob(params: JobParameters?): Boolean {
          CoroutineScope(Dispatchers.IO).launch {
              // Fire and Forget
          }
          return false
      }
  
      override fun onStopJob(params: JobParameters?): Boolean {
          return false
      }
  }
  </code></pre>
  <h3>Questions</h3>
  <p>If you want to answer please use the number of the question.</p>
  <ol>
  <li>Is CoroutineScope a good idea?</li>
  <li>I'm getting errors from the main thread &quot;I/art: Note: end time exceeds epoch:&quot; - this is confusing and could indicate that the JobService does to much work on the UI thread. But I have no errors from the choreographer which should normally be the case if the UI is in trouble.</li>
  <li>Is it a bad idea to return false in the JobService functions</li>
  </ol>
  <h3>Currently used Links</h3>
  <p><a href="https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb" rel="nofollow noreferrer">https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb</a>
  <a href="https://medium.com/androiddevelopers/coroutines-on-android-part-ii-getting-started-3bff117176dd" rel="nofollow noreferrer">https://medium.com/androiddevelopers/coroutines-on-android-part-ii-getting-started-3bff117176dd</a>
  <a href="https://medium.com/androiddevelopers/coroutines-on-android-part-iii-real-work-2ba8a2ec2f45" rel="nofollow noreferrer">https://medium.com/androiddevelopers/coroutines-on-android-part-iii-real-work-2ba8a2ec2f45</a>
  <a href="https://developer.android.com/kotlin/coroutines/coroutines-adv?hl=de#coroutinescope" rel="nofollow noreferrer">https://developer.android.com/kotlin/coroutines/coroutines-adv?hl=de#coroutinescope</a>
  <a href="https://kotlinlang.org/docs/coroutines-basics.html#extract-function-refactoring" rel="nofollow noreferrer">https://kotlinlang.org/docs/coroutines-basics.html#extract-function-refactoring</a></p>