Fetch: hasActiveDownloads returning false even when there is enqueued downloads

Created on 7 Dec 2018  路  5Comments  路  Source: tonyofrancis/Fetch

Hi Tony.

I'm iterating through a list of URLs and for each item I'm calling fun enqueue(request: Request): Convertible. Then I'm listening to the progress updates using FetchListener. The download and progress updates are working fine, but I'm having problems using hasActiveDownloads. This flag is always false when requested after onCompleted even though I still have more enqueued downloads. The flag is true while receiving onProgress updates.

This is my configuration settings.

val client = OkHttpClient.Builder()
                .callTimeout(5, TimeUnit.SECONDS)
                .readTimeout(5, TimeUnit.SECONDS)
                .writeTimeout(5, TimeUnit.SECONDS)
                .connectTimeout(5, TimeUnit.SECONDS)
                .followRedirects(true)
                .followSslRedirects(true)
                .build()

        val downloader = OkHttpDownloader(client, Downloader.FileDownloaderType.SEQUENTIAL)

        val fetchConfiguration: FetchConfiguration = FetchConfiguration.Builder(context)
                .setNotificationManager(null)
                .setNamespace("my.namespace")
                .setProgressReportingInterval(1000)
                .setDownloadConcurrentLimit(1)
                .setHttpDownloader(downloader)
                .build()

        fetch = RxFetch.getRxInstance(fetchConfiguration)
enhancement

Most helpful comment

@gbirk @jpvs0101 Fetch 2.3.4 has be released. hasActiveDownloads has been updated to correctly return true when there are downloads with a downloading or queued status. Note: this method cannot be called on a ui thread.

Also add new awaitFinish() and awaitFinishTimeout() methods that block a background thread until Fetch completes all queued downloads. No need to create your own loops to check. Note: this method cannot be called on a ui thread or else an exception is thrown.

All 5 comments

@gbirk hasActiveDownloads only returns if a download is activity downloading. The name of the method is miss leading and has to be updated. I will be working on this to include queued downloads as well. Not sure when I will have a fix but will post back soon.

Useful if you add something like hasAnyActiveOrUpcomingDownloads()

Most of the time we check this for shutdown the service.

@gbirk @jpvs0101 Fetch 2.3.4 has be released. hasActiveDownloads has been updated to correctly return true when there are downloads with a downloading or queued status. Note: this method cannot be called on a ui thread.

Also add new awaitFinish() and awaitFinishTimeout() methods that block a background thread until Fetch completes all queued downloads. No need to create your own loops to check. Note: this method cannot be called on a ui thread or else an exception is thrown.

Hi Tony. Quick question, do we have an option to call for an Observable or Flowable on this?

Sorry I'm a bit late on this, but I saw that FetchHandlerImpl throws an exception if the call is done on the main thread.
override fun hasActiveDownloads(): Boolean { if (Thread.currentThread() == Looper.getMainLooper().thread) { throw FetchException(BLOCKING_CALL_ON_UI_THREAD) } return databaseManager.getPendingCount() > 0 }

but RxFetchImpl catches the exception and returns false.
override val hasActiveDownloads: Boolean get() { return try { fetchHandler.hasActiveDownloads() } catch (e: Exception) { false } }
I think this might create some confusion with hasActiveDownloads returning false because the contract is not being followed correctly. Maybe just propagate the exception?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gbirk picture gbirk  路  6Comments

jpvs0101 picture jpvs0101  路  6Comments

DeevD picture DeevD  路  3Comments

DeevD picture DeevD  路  3Comments

cepero91 picture cepero91  路  4Comments