kesha-antonov / react-native-background-downloader

About A library for React-Native to help you download large files on iOS and Android both in the foreground and most importantly in the background.
https://www.npmjs.com/package/@kesha-antonov/react-native-background-downloader
Other
26 stars 3 forks source link

[IOS] Latency of Download in background with locked screen #5

Closed khbeieb closed 2 months ago

khbeieb commented 2 months ago

Is this a bug report, a feature request, or a question?

Question

Is the bug specific to iOS or Android? Or can it be reproduced on both platforms?

iOS (all versions)

Environment

Environment: React: 18.2.0 React native: 0.72.7 react-native-background-downloader: 3.1.1

Target Platform: iOS (13.0)

Expected Behavior

We are using this library to Download multiple number of files in the foreground/background consecutively. Expected behaviour: download works fine in all modes (foreground, background and with a locked screen)

Actual Behavior

When transitioning to background mode, the download operates smoothly. However, upon locking the screen, we've noticed a significant decrease in download latency, with the download almost ceasing altogether.

PS: we are testing with real devices.

Are there any restrictions imposed by the operating system, or is it possible that this library is unable to handle the locked screen mode?

Rania-TIT commented 2 months ago

the same issue @kesha-antonov any idea ? please.

kesha-antonov commented 2 months ago

I'll check apple docs but I think it's system limitations since it controls battery usage etc

khbeieb commented 2 months ago

Actually, after performing more tests, we found out that the background download is not working on most iOS devices.

Example: iphone 14 pro with iOS version 17.3.1 does not work.

Each time the app goes to backgound mode, the download is suspended, and whenever the app comes up to the foreground, the download resumes directly.

I checked for all limitations like low battery mode, closed all apps opened in app drawer etc...

kesha-antonov commented 2 months ago

How many downloads do you enqueue? Are downloads not working on native side or js side? Because js really gets paused in react native as I understand when app in the background

You can enqueue downloads and when app opens get downloaded files

kesha-antonov commented 2 months ago

Also read Apple docs for details https://developer.apple.com/documentation/foundation/url_loading_system/downloading_files_in_the_background

khbeieb commented 2 months ago

Meaning when i start the download in foreground, the files downloaded is 2/100 for example. when i go to background mode and return to the application after 30 minutes for example, i find that it bacame 3/100, and then the download continues normally with a good speed.

kesha-antonov commented 2 months ago
  • How many downloads do you enqueue? -> It depends, but mostly 100 downloads of low size files. I am enqueuing the download recursively from the JS side, should i enque them in the native side ?

That's not good practice by apple docs.

From the link I sent:

Use background sessions efficiently When the system resumes or relaunches your app, it uses a rate limiter to prevent abuse of background downloads. When your app starts a new download task while in the background, the task doesn't begin until the delay expires. The delay increases each time the system resumes or relaunches your app.

As a result, if your app starts a single background download, gets resumed when the download completes, and then starts a new download, it will greatly increase the delay. Instead, use a small number of background sessions — ideally just one — and use these sessions to start many download tasks at once. This allows the system to perform multiple downloads at once, and resume your app when they have completed.

Keep in mind, though, that each task has its own overhead. If you find you need to launch thousands of download tasks, change your design to perform fewer, larger transfers.

Note

The delay is reset to 0 whenever the user brings your app to the foreground. It also resets if the delay period elapses without the system resuming or relaunching your app.

kesha-antonov commented 2 months ago

It's better either to zip your files on server and make it one big file, or enqueue 100 downloads at once

When you'll open your app you'll have files downloaded

khbeieb commented 2 months ago

Thank you @kesha-antonov