简体   繁体   中英

Why is onDestroy() being called as soon as Intent Service Starts?

I know that a Intent Service ends as soon as it's work is complete. I am making network calls on onHandleIntent().The service dies as soon as it starts but the network calls complete successfully.

Is it because all the methods for network calls are called and they exist in a different thread? So, the service dies?

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    Log.i(TAG, "Download Service Started!");
    initVariables();
    startDownloadService(intent);
}

private void startDownloadService(Intent intent) {
    receiver = intent.getParcelableExtra("receiver");
    notifyDownloadRunning("Trying to start Download");

    getNews();
    getVideoDetails();
    .................
}

Retorfit Interface

@GET()
Observable<VideoDetailsRoot> getVideoDetails(@Url String url);

You're handling the threading twice. Once with the IntentService and once with Retrofit and Rx.

When subscribing to an Observable , you'll not block the current thread (most of the time) and instead wait for the result asynchronously.

In your case you can skip the implementation of the IntentService with good conscience. Retrofit and Rx provide you with enough capabilities to handle downloading asynchronously without blocking the main thread.

If you want to keep the service, you need to make the networking part synchronous or wait for the subscriptions to complete. But any of this might be a miss-use of Retrofit itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM