1

I created an app where user can download multiple images on one click using react-native-fs which is working perfectly in Android. But in iOS when app is inactive then download stopped and user have to start download again.

async.eachSeries(DownloadData, async function (tourData, finish) {
    console.log("# resumable : 655612", tourData);
    var fileExtension = '';
    var fileURL = tourData.path;
var fileExtension = "/" + tourData.name + "Image" + p + ".png
     p = p + 1;
    const downloadDest = RNFS.DocumentDirectoryPath + fileExtension;
    let dirs = RNFetchBlob.fs.dirs;
    var v = dirs.DocumentDir;
    var jobId = -1;

    const ret = RNFS.downloadFile({
        fromUrl: encodeURI(fileURL),
        toFile: downloadDest,
        connectionTimeout: 1000 * 10,
        readTimeout: 1000 * 10,
        background: true,
        discretionary: true,
        progressDivider: 1,
        resumable: (res) => {
            console.log("# resumable", res);
        },
        begin: (res) => {
         console.log(res)
        },
        progress: (data) => {
           console.log(data)
        },
    });

    jobId = ret.jobId;

    RNFS.isResumable(jobId).then(true);

    if (await RNFS.isResumable(jobId)) {
        console.log("# resumable : # resumable : # resumable :",jobId);
        RNFS.resumeDownload(jobId)
    }
    ret.promise.then((res) => {
       finish();
   }).catch(err => {
       finish();
   })

},function (err) { if (!err) { callback(true) } else { callback(false) } }));

Sandeep Mishra
  • 650
  • 2
  • 9
  • 20

1 Answers1

0

Running download in background in IOS require few extra settings check this section https://github.com/itinance/react-native-fs#background-downloads-tutorial-ios they also mentioned that IOS will give you 30 sec after handleEventsForBackgroundURLSession

BE AWARE! iOS will give about 30 sec. to run your code after handleEventsForBackgroundURLSession is called and until completionHandler is triggered so don't do anything that might take a long time (like unzipping), you will be able to do it after the user re-launces the app, otherwide iOS will terminate your app.

I hope this helps

Rachid Rhafour
  • 436
  • 2
  • 9