Skip to content

iOS: Some files are saved with 0 KB size when IOSBackgroundTask: true #424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
eduardo-santos-tribia opened this issue Jun 5, 2025 · 2 comments

Comments

@eduardo-santos-tribia
Copy link

eduardo-santos-tribia commented Jun 5, 2025

Hi, I'm using react-native-blob-util in a React Native CLI app to download multiple files in parallel. The downloads work perfectly when IOSBackgroundTask is set to false, but when I set IOSBackgroundTask: true, I consistently encounter a problem: some of the downloaded files end up being 0 KB in size, and it's not always the same file. The issue appears to occur randomly.

Can someone help me understand why this is happening, and when should I set IOSBackgroundTask to true? The App I'm building has a requirement to work Offline, that's why I have to download multiple files and store them. It also need to keep downloading the files even when the App is running on background.

Environment:

Library version: react-native-blob-util@^0.19.11

React Native version: 0.76.9

Platform: iOS

Device: Simulator (tested so far)

iOS Version: e.g., iOS 17.5 (simulator)

Development machine: macOS

Steps to reproduce:

Configure the fetch call with IOSBackgroundTask: true.

Initiate multiple file downloads in parallel (e.g., 5–10).

Observe that some files are saved with 0 KB size.

Repeat test — different files result in 0 KB on each run.

Sample Code

const fetchBlob = async (
  fileName: string,
  fullFilePath: string,
) => {
  const authBearerToken = `Bearer token`
  const fullApiRoute = 'MyApiEndpoint'

  const headers = { Authorization: authBearerToken }

  return new Promise<any>((resolve, reject) => {
    ReactNativeBlobUtil.config({
      path: fullFilePath,
      fileCache: true,
      indicator: true,
      overwrite: true,
      IOSBackgroundTask: true, // works when is set to false.
    })
      .fetch('GET', fullApiRoute, headers)
      .then(async res => {
        const resStatus = res?.info ? res.info().status : 'Unknown'

        const isValidResponse = resStatus === 200 || res.data

        if (isValidResponse) {
          resolve(res)
        }
      })
      .catch(error => {
        reject(error)
      })
  })
}

// Downloading multiple files in parallel
let successCount = 0
const allPromises = await Promise.all(
  filesToFetch.map(file => {
    return fetchBlob(file.name, file.pathToDownload)
      .then(() => {
        successCount++
        return 'done'
      })
      .catch(err => {
        console.warn(`Skipping file due to error for ${file.id}:`, err)
        return 'error'
      })
  }),
)

Screenshot

Image

@RonRadtke
Copy link
Owner

Is the app in the background when the downloads finish?
Do you get a positive response for these files that have 0 bytes?

What happens if you don't use the file cache?

I'm wondering if it maybe downloads the file but since the app is not active it stops the processing and does not handle the finished download correctly

@eduardo-santos-tribia
Copy link
Author

eduardo-santos-tribia commented Jun 6, 2025

Hi @RonRadtke , thanks for the reply. Let me try to answer your questions:

1. Is the app in the background when the downloads finish?
No, the App is still running and open when the downloads finish.

2. Do you get a positive response for these files that have 0 bytes?
Yes, the response is HTTP 200 code, but I get an error if I try to load this file.

3. What happens if you don't use the file cache?
Still the same issue, the only way to make my parallel downloads work is setting IOSBackgroundTask to false.

4. I'm wondering if it maybe downloads the file but since the app is not active it stops the processing and does not handle the finished download correctly
In my case, I'm not putting the App on Background, even though this is a requirement for my App, I'm still not doing this because I want to ensure and validate that all Downloads work when the app is active first. What I think is happening might be related to the way iOS handles the parallel downloads when IOSBackgroundTask is set to true. I mean, probably there are some limitations by the OS related to the amount of parallel downloads or the size of the downloads, but it's still strange that every time I try this parallel downloads I have a different response. Sometimes all the 6 files are downloaded with correct size and sometimes one and/or another have 0 bytes.

Also, it's strange that the response from react-native-blob-utils is success and valid even if the file was not properly created. I think in this case, something got stuck in the layer between react-native-blob-utils and iOS native, but I'm not sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants