0

I am using Firebase Job Dispatcher and setting it up like described in https://github.com/firebase/firebase-jobdispatcher-android/blob/master/README.md.

The job to be scheduled is a http request. The http request could run while the app is on the foreground (the user is looking at it), or while the app is in the background/not started.

Should the http request (using the okhttp library) be put in a separate thread / asynchronous or can I just call it directly from the JobService?

ʕ ᵔᴥᵔ ʔ
  • 772
  • 1
  • 8
  • 14

1 Answers1

3

JobService extends Service so it runs on the main thread. Therefore, you shouldn't be able to make network calls directly. However, there is SimpleJobService where you can make direct calls.

I think using SimpleJobService is better than using AsyncTask within JobService because it already does it in its own way as you can see here.

Mehmed
  • 2,880
  • 4
  • 41
  • 62
  • Thanks for mentioning the JobService class itself. I checked the Documentatieonderdelen on JobService which states: "This service executes each incoming job on a Handler running on your application's main thread. This means that you must offload your execution logic to another thread/handler/AsyncTask of your choosing. Not doing so will result in blocking any future callbacks from the JobManager - specifically onStopJob(android.app.job.JobParameters), which is meant to inform you that the scheduling requirements are no longer being met." So the network call must have its own thread. – ʕ ᵔᴥᵔ ʔ Sep 28 '17 at 19:03
  • 1
    That's right. As for OkHttp, calling the asynchronous `enqueue()` method inside `JobService` should suffice. In `SimpleJobService`, the synchronous `execute()` method can be called directly since `SimpleJobService` does the given job in a separate `AsyncJobTask`. – Mehmed Sep 28 '17 at 20:15
  • +1 for SimpleJobService. I found it in the source code at https://github.com/firebase/firebase-jobdispatcher-android/. How did you find this class? In the README.md on their github account, I could not find any API documentation, or even a javadoc. Do you have more information on this? – ʕ ᵔᴥᵔ ʔ Sep 29 '17 at 06:48
  • I think I first came across in the I/O presentation link given in the readme: https://youtu.be/VC2Hlb22mZM?t=30m23s. Answer to 10th question in [this answer](https://stackoverflow.com/a/39412775/876267) was also helpful. – Mehmed Sep 29 '17 at 15:09