【问题标题】:AWSS3 Mobile SDK cancel file download in swiftAWSS3 Mobile SDK 快速取消文件下载
【发布时间】:2019-12-30 11:51:53
【问题描述】:

我的应用程序中有一个使用 AWS 移动开发工具包的文件下载代码。 这会显示一个带有下载进度的警报和一个取消按钮。它正在工作,但是当用户点击警报取消按钮时,我需要取消下载。我尝试使用 AWSS3TransferUtilityDownloadTask 和 AWSS3TransferUtilityTask 的对象取消任务。但它似乎不起作用,它给了我进度和下载完成。

这里是我的文件下载工作代码。

请建议我如何取消我的下载请求。

    let credentialProvider = AWSStaticCredentialsProvider(accessKey: "MyAccessKey", secretKey: "MySecretKey")
    let configuration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: credentialProvider)
    AWSServiceManager.default().defaultServiceConfiguration = configuration

    let expression = AWSS3TransferUtilityDownloadExpression()
    expression.progressBlock = {(task, progress) in DispatchQueue.main.async(execute: {
        // Do something e.g. Update a progress bar.
        print("File Progress:",Float(progress.fractionCompleted))

        let progress = Float(progress.fractionCompleted)

        DispatchQueue.main.async {
            if progress == 1.0 {
                self.globalAlert.dismiss(animated: true, completion: nil)
            }else{
            self.globalProgressView!.progress = progress
            self.msgProgress = String(Int(progress*100))
            }
        }


    })
    }

    var completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock?
    completionHandler = { (task, URL, data, error) -> Void in
        DispatchQueue.main.async(execute: {
            // Do something e.g. Alert a user for transfer completion.
            // On failed downloads, `error` contains the error object.
            //My other code stuff execution

        })
    }
    var refUploadTask: AWSS3TransferUtilityTask?
    let transferUtility = AWSS3TransferUtility.default()

    transferUtility.downloadData(
        fromBucket: Bucket_Name,
        key: fileKey,
        expression: expression,
        completionHandler: completionHandler
        ).continueWith {
            (task) -> AnyObject? in if let error = task.error {
                print("Error: \(error.localizedDescription)")
            }

            if let _ = task.result {
                // Do something with downloadTask.
                print("download started..")
                DispatchQueue.main.async(execute: {

                    self.globalAlert = UIAlertController(title: "Downloading...", message: "\(String(describing: self.msgProgress))% Completed", preferredStyle: .alert)
                    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: {
                        (action : UIAlertAction!) -> Void in


                             self.refUploadTask.cancel()


                    })

                    //  Show it to your users
                    self.globalAlert.addAction(cancelAction)
                    self.present(self.globalAlert, animated: true, completion: {
                        //  Add your progressbar after alert is shown (and measured)
                        let margin:CGFloat = 8.0
                        let rect = CGRect(x: margin, y: 72.0, width: self.globalAlert.view.frame.width - margin * 2.0 , height: 2.0)
                        self.globalProgressView = UIProgressView(frame: rect)
                        self.globalProgressView!.tintColor = .blue
                        self.globalAlert.view.addSubview(self.globalProgressView!)
                    })
                })

            }
            return nil;
    }

【问题讨论】:

    标签: ios amazon-s3 aws-sdk swift5 downloadfile


    【解决方案1】:

    您可以获取 AWSS3TransferUtilityDownloadTask 对象并取消正在进行的下载。

     let awsTask = transferUtility.getDownloadTasks()
        
        if let taskArray = awsTask.result {
                 
                  for idx in taskArray {
                      
                      if let task = idx as? AWSS3TransferUtilityDownloadTask {
                        
                        
                        task.cancel()
                    }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-28
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多