Rn-fetch-blob: Problem with canceling the request

Created on 7 Aug 2018  路  9Comments  路  Source: joltup/rn-fetch-blob

Specs:
"react-native": "0.55.4",
"rn-fetch-blob": "0.10.12",

Hello guys :)

So I have a problem with canceling request. I'm trying to download some huge apk file (more than 400 MB) and I want to give user ability to cancel the download process.

So I'm creating the request in this way:

let request = RNFetchBlob.config({
             path: apkPath,
             fileCache: true
        }).fetch('GET', app.apkDownloadUrl)
           .then((res) => {[...]});

Then, when I call:

request.cancel();

There is error that says that "cancel" is not a function.
What I'm doing wrong? :)

BTW: Beside this issue, great library!

Most helpful comment

I have an issue maybe is a bug, I don't know. When I request a cancel the promise of success is triggered.

this.task = config(options).fetch('GET', url);
            this.task.progress((received, total) => {
                let progress_value = received / total;
                this.setState({ progress_value });
            });
            this.task.then((res) => {
                // Success handle - When I call _cancelDownload() this promise is triggered
                }).catch(err => console.log(err))  
            });
        }
    }

    _cancelDownload() {

        this.task.cancel((err, taskId) => {
            // Handle cancl
        });

    }

All 9 comments

Try to put in request
RNFetchBlob.config({ path: apkPath, fileCache: true }).fetch('GET', app.apkDownloadUrl)
and after
request.then((res) => {[...]}); request.cancel();

I have an issue maybe is a bug, I don't know. When I request a cancel the promise of success is triggered.

this.task = config(options).fetch('GET', url);
            this.task.progress((received, total) => {
                let progress_value = received / total;
                this.setState({ progress_value });
            });
            this.task.then((res) => {
                // Success handle - When I call _cancelDownload() this promise is triggered
                }).catch(err => console.log(err))  
            });
        }
    }

    _cancelDownload() {

        this.task.cancel((err, taskId) => {
            // Handle cancl
        });

    }

Same issue on Android as @ithustle explained, and i do not know if the issue also happens on iOS.

@HasanAlyazidi only on Android. On IOS is working propertly

I managed to solve it with this workaround

    ...
    this.isCancelled = false; // <-- step 1 (needed if you want to retry downloading)

    this.task = config(options).fetch('GET', url);

    this.task.progress((received, total) => {
        let progress_value = received / total;
        this.setState({ progress_value });
    });

    this.task.then((res) => {
        if (this.isCancelled) return;  // <-- step 2

        // save to db/redux, etc...
    })
    .catch(err => console.log(err))
    ...

    _cancelDownload() {
        this.isCancelled = true; // <-- step 3

        this.task.cancel((err, taskId) => {
            // Handle cancelling
        });
    }

I have an issue maybe is a bug, I don't know. When I request a cancel the promise of success is triggered.

this.task = config(options).fetch('GET', url);
            this.task.progress((received, total) => {
                let progress_value = received / total;
                this.setState({ progress_value });
            });
            this.task.then((res) => {
                // Success handle - When I call _cancelDownload() this promise is triggered
                }).catch(err => console.log(err))  
            });
        }
    }

    _cancelDownload() {

        this.task.cancel((err, taskId) => {
            // Handle cancl
        });

    }

Same problem here
calling cancel when download is in middle of it, it will trigger success promise and going into then function

Experiencing the same issue.
Canceling a download in iOS produced a promise rejection.
Canceling a download with android returns success and no indication of the cancellation.
I'm currently using the workaround posted by @HasanAlyazidi which works fine, but it would be so much nicer to have the library behaving consistently =)

Same issue as @ithustle explained. And @HasanAlyazidi 's workaround may face the problem when the download is canceled because of network error, not canceled by hand.

I think this PR #381 fixes it.

Was this page helpful?
0 / 5 - 0 ratings