2

i'm using react-native fs to download content from a CMS into an react-native app. The idea is to use the app to serve the online content/website for offline use. The app does well on Android without throwing any errors, but on the iPad (ios 11.4) the download never finishes. Here's part of the App.js that i'm using with the function, that is throwing the error on the iPad:

import React, {Component} from 'react'
import {
Platform,
StyleSheet,
Text,
View,
WebView,
Button,
Alert,
NetInfo,
StatusBar,
Modal,
TouchableOpacity,
Image
} from 'react-native'

import RNFS from 'react-native-fs'

export default class App extends Component {
    const path = RNFS.DocumentDirectoryPath + '/assets/' + saveAs
    let pp = path.split('/')
    pp.pop()

    return RNFS.mkdir(pp.join('/'), {NSURLIsExcludedFromBackupKey: true}).then(() => {
        const download = RNFS.downloadFile({
            fromUrl: url,
            toFile: path,
            discretionary: true,
            progress: (res) => {
                const tmp = this.state.bytesWritten
                tmp[res.jobId.toString()] = res.bytesWritten
                this.setState({bytesWritten: tmp})
            },
            begin: (res) => {
                this.setState({
                    contentLength: this.state.contentLength + res.contentLength
                })
            },
            readTimout: 15000
        })
        return download.promise.then((res) => {
            return res;
        })
        .catch((error) => {
            console.warn('An error occured while downloading: ' + error);
            console.warn('Current URL: ' + url);
        });
    })
    .catch ((error) => {
        console.warn('An error occured while creating directory: ' + error);
    });
}

The function downloadAndSave is called within another function like this:

let pr = []
// d are the files that need to be downloaded, around 250-300
d.forEach((elem) => {
    pr.push(this.downloadAndSave(elem.name, elem.saveAs));
})
return Promise.all(pr);

The error(s) always come from the catch-block 'An error occured while downloading:'. The error thrown differs as well as the number of affected URLs.

Error Messages that occure are (Translated, so they may be a bit off):

  • Request/Process could not be finished
  • Request time out
  • POSIX-Error -9805 - Unknown error: -9805

The error occures after downloading around 50-70% of the files and then never finished but stops at around 80-90%.

My question is if the problem is related to time out or possible to many callbacks? Has anyone else encountered such a problem with react native fs or has someone an idea how to solve this problem. I am afraid i can't even really pinpoint the problem. Any help is deeply appreciated!

1 Answers1

0

have you tried adding background: true to RNFS.downloadFile({background: true})'s options ?

Rachid Rhafour
  • 426
  • 2
  • 9