Better management of requireWifi
at the application level for download tasks
#226
Replies: 5 comments 1 reply
-
Seems reasonable to me. Can the native code intercept the OS pausing a task because of a (missing) WiFi connection? Because if that is so, you wouldn't need to preemptively restart tasks if the global toggle changes, you could simply wait until the task is paused and then update and resume it. That would mean that if the network conditions don't change, a task without WiFi-only requirement can just keep running... Another thing, why does this only apply to downloads? I'm sure there are use cases for uploading only via WiFi, e.g. for a backup application like immich. So unless there are technical reasons why it's not possible, I'd treat uploads the exact same. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the feedback and ideas. Unfortunately, when wifi is required and becomes unavailable, then iOS pauses without any way to detect that, and Android bluntly cancels the taskworker. Maybe the Android case can be twisted to do what you suggest, but not iOS. Uploads are not supported because pause/resume is currently not supported for uploads. If that were to change (no plans!) then I agree this could apply similarly there. |
Beta Was this translation helpful? Give feedback.
-
@Chaphasilor @kodomo5197 I implemented this on branch 'wifi' of the background_downloader repo. Use |
Beta Was this translation helpful? Give feedback.
-
This has been implemented roughly as described in this discussion, in version 8.2.0 |
Beta Was this translation helpful? Give feedback.
-
@781flyingdutchman is there a way for you to detect metered networks on desktop platforms (and also mobile I guess)? That might be a good addition to prevent excessive downloading in some scenarios even on desktop. And on mobile, it would be nice if downloads would also be prevented if the "Data Saver" is enabled, although that might already be handled automatically by the OS? |
Beta Was this translation helpful? Give feedback.
-
Although each task can specify whether it requires WiFi to run or not, this mechanism does not address the situation where a user switches a global toggle to 'Download over WiFi only' when tasks are already running. Because they were enqueued with whatever
requiresWiFi
setting, they'll stay like that. To the end user, this seems wrong.This proposal introduces a new method to set
requiresWiFi
globally, across all download tasks, including those already enqueued or running.Introduce a new method
FileDownloader().requireWifi(RequireWifi when)
, withRequireWifi
and enum with valuesforAllTasks
,forNoTasks
andasRequestedByTask
. The action of this call only affects downloads, not uploads, and only affects iOS and AndroidasRequestedByTask
and in that case the requirement for WiFi is dictated by therequiresWiFi
field of theTask
forAllTasks
orforNoTasks
overrides any task-specific settings at the moment the task is enqueuedFileDownloader().requireWifi(RequireWifi when)
, then on the native side two things happen:when
value dictates), we cancel the task and re-enqueue with the newwhen
setting. This will be invisible to the user, and to the developer in that no state changes will be sent (we'll intercept those on the native side)when
value dictates), we pause the task and immediately resume it with the newwhen
setting (a resume is really an enqueue with extra resume data). The task will emit state changesenqueued
, thenrunning
once WiFi conditions are met. If resume fails (uncommon for long downloads, but always if the task'sallowPause
is false), the task is cancelled and re-enqueued (emittingcanceled
andenqueued
states) and will start downloading from the beginning oncerunning
FileDownloader().requireWifi(RequireWifi when)
is a Future with a list of tasks that are now waiting for WiFi as a result of the call - in case you want to act on that information in the app's UI or via a notification. We could expose another method to directly set or cancel the notification for a task so you can say something like 'Waiting for WiFi', or add anotherTaskNotification
typewaitingForWiFi
to theFileDownloader().configureNotification(...)
method that automatically shows that notification when a task is waiting for WiFi. I don't intend to ask aTaskStatus
value forwaitingForWiFi
as I consider it a form ofenqueued
and don't want to introduce a breaking change.A few observations:
requiresWiFi
field changed, so it may not reflect the actual WiFi requirement used during downloads.FileDownloader().requireWifi(RequireWifi.forAllTasks)
will pause the task, then resume it with the new condition that WiFi be available. This seems odd, given we were happily downloading on WiFi anyway, but it's required to allow the native system to pause or fail the task when moving out of WiFi range. There's a small chance the resume fails, and spoils a perfectly fine download, but I think this is acceptable, given you've explicitly requested that tasks require WiFi.FileDownloader().requireWifi(RequireWifi.forNoTasks)
removes the condition, but pauses and resumes the task to do so, with the same small risk of failurepaused
(orfailed
) state (because we intercept it) but we should not suppressenqueued
andrunning
as the task may not get back torunning
for a while. Ideally there is awaiting for WiFi
state, but this is breaking and complicated to implementBeta Was this translation helpful? Give feedback.
All reactions