apache / apache/cordova-plugin-file-transfer

Longer downloads fail silently when app is backgrounded on iOS

Open
#353 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
606
Forks
874
PR merge metrics
No merged PRs in 30d

Description

# Bug Report

## Problem

Reproduce:

* Start a download that runs approx. longer than 30s.
* Background the app (i.e. home button, open other app)
* Be on battery (no cable) for quicker fails. Also iPhones fail quicker than iPads. YMMV.

### What is expected to happen?

* The download continues until it's done

### What does actually happen?

* The download fails
* The file is not created
* neither success nor error handlers are called

## Information

* This seems to be a common issue for background tasks on cordova. Hence the existence of the [background mode plugin](https://github.com/katzer/cordova-plugin-background-mode), which doesn't work any more with modern OSes. See also Android doze.
* I thought it might not apply because i see several references to `backgroundTaskID` etc. in the code, which the [general docs about background execution](https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/extending_your_app_s_background_execution_time?language=objc) talk about. But I'm too unfamiliar with iOS dev to really tell.
* I thought the "background fetch" capability would solve this, but nope. Might need [a specific implementation](https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/using_background_tasks_to_update_your_app?language=objc) to work.
* This is somewhat related to #213, essentially a duplicate of [the issue described in this comment](https://github.com/apache/cordova-plugin-file-transfer/issues/213#issuecomment-628043448), but its own issue -- resumable downloads could mitigate / work around, but not solve the core issue.
* Seems not to affect Android, but I might have to test harder (longer downloads)
* I will give https://github.com/sgrebnov/cordova-plugin-background-download a try, but I am not getting my hopes up
* At the very least, the error callback should fire.
* For now I guess I'll have to add a hint: "Please don't close the app while download is running"... (or remember that I had started a download, an then *onforeground* check if it is still running, check if the file is there... oof, heuristic headaches)

### Environment, Platform, Device

I think only iOS, Android needs more testing.

### Version information

* cordova 11.0.0
* cordova-ios 6.2.0
* current dev version of this plugin 7ba6fa375 (but also 5ed73ae0)
* cordova-plugin-file 7.0.0 (but also 5.0.0)
* Xcode 13.1
* iOS 15.4.1, 16(.1 iirc), 12.3.1 (I *think*. It looks like that one has just trained itself for longer exec times, *sigh*)

## Checklist

- [x] I searched for existing GitHub issues
- [x] I updated all Cordova tooling to most recent version
- [x] I included all the necessary information above

Contributor guide

Open the contributing guide

Research direction

Start by tracing the iOS implementation around the `backgroundTaskID` references and reproduce a download longer than 30 seconds while the app is backgrounded on battery. Compare the download and callback behavior before and after backgrounding; done means the download completes or the error callback fires instead of failing silently.

Written by the indexing model from the issue text.

Assessment

Tech stack
ios, javascript, objective-c
Domain
mobile-dev
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.