Skip to content
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

Cancel Method not working reliably on iOS #92

Closed
majirosstefan opened this issue Dec 22, 2021 · 1 comment
Closed

Cancel Method not working reliably on iOS #92

majirosstefan opened this issue Dec 22, 2021 · 1 comment

Comments

@majirosstefan
Copy link

majirosstefan commented Dec 22, 2021

When trying to cancel a small file download, it happens pretty often that the cancel method call gets ignored.

const download = async () => {
  const hasPermission = await getPermissionToDownload();
  if (hasPermission) {            
      setDownloadModalVisible(true);
      let DownloadDir = ReactNativeBlobUtil.fs.dirs.DownloadDir;
      let name = props.message.data.attachments[0].name;        
    
       let task = ReactNativeBlobUtil.config({          
        fileCache: true,
        appendExt: props.message.data.attachments[0].extension,
        addAndroidDownloads: {
          useDownloadManager: true,
          notification: true,
          path: DownloadDir + "/" + name,
        },
      }).fetch("GET", url, { });
                              
      downloadTask = task;                         
      
      await sleep(5000);
      
      task.then( res => {
        setDownloadModalVisible(false);
        console.log("res: ", res, JSON.stringify(res));                                            
       }).catch(err => {
        setDownloadModalVisible(false);
        console.log("Could be cancelledd", err);                  
       });                 
  } else {
    console.log("Storage permission denied");
  }
};

As you can see, I am opening the modal where I intend to put the progress indicator and cancel button that calls the following function that closes modal, and should pause download:

const onCancelDownload =  async () => {
    try {      
      console.log("Cancelling", downloadTask)
      await downloadTask.cancel(() => "canceled");
      setDownloadModalVisible(false);                
      downloadTask = null;      
    } catch(err){
      console.log(err)
    }    
  }

Also,

let downloadTask = null; 

is placed outside functional component that has onCancelDownload, and download methods.

However, it happens pretty often, that after calling the 'downloadTask.cancel', nothing happens, and downloading continues.
But sometimes it does resolve properly, as I would expect (that the canceled error is thrown).

Dependencies: "react-native": "~0.63.4", "react": "16.13.1", "react-native-blob-util": "^0.13.18",

@RonRadtke
Copy link
Owner

This might be related to: #130

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