Why alamofire downloads progress in alert will flashing and the behind screen starts to be fade to black?

喜你入骨 提交于 2020-02-05 04:55:05

问题


I have progress view in alert when downloading with alamofire, but the problem is that when I want to download the progress view in alert will start to be complete but flashing and the other problem is that the behind screen will start to be fade to black I think in every second a new alert with progress view will show and at the end there are lots of alerts how can I avoid them?

downloadTimer = Timer.scheduledTimer(timeInterval: 1 , target: self, selector: #selector(flashingDownload), userInfo: nil, repeats: true)

        let destination = DownloadRequest.suggestedDownloadDestination()

        Alamofire.download("http://example.com", to: destination).downloadProgress(queue: DispatchQueue.global(qos: .utility)) { (progress) in



            print("Progress: \(progress.fractionCompleted)")

            sixthLevelViewController.progressdownload = Float(progress.fractionCompleted)

            if sixthLevelViewController.progressdownload == 1.0 {

                self.downloadfinished = true

                let alertController = UIAlertController(title: "finish download   ", message: " download Completed! ", preferredStyle: UIAlertControllerStyle.alert)

                let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default)
                {
                    (result : UIAlertAction) -> Void in
                    self.view.isUserInteractionEnabled = true

                }
                alertController.addAction(okAction)
                self.present(alertController, animated: true, completion: nil)

                alertController.view.tintColor = UIColor.green
                alertController.view.backgroundColor = UIColor.green
                alertController.view.layer.cornerRadius = 0.1 * alertController.view.bounds.size.width


            }


            } .validate().responseData { ( response ) in
                print(response.destinationURL!.lastPathComponent)
        }

and here is the codes for timer

  func flashingDownload() {

  while (topViewController.presentedViewController != nil){
            topViewController = topViewController.presentedViewController!
        }

        DispatchQueue.main.async {
            let alert = UIAlertController(title: "downloading", message: "pls wait", preferredStyle: .alert)
            let progressBar = UIProgressView(progressViewStyle: .default)
            progressBar.setProgress(sixthLevelViewController.progressdownload, animated: true)
            progressBar.frame = CGRect(x: 10, y: 70, width: 250, height: 0)
            alert.view.addSubview(progressBar)
            self.topViewController.present(alert, animated: false, completion: nil)


            if sixthLevelViewController.progressdownload == 1.0 {

                print("finished download !")
                self.topViewController.dismiss(animated: false , completion: nil)
                self.downloadTimer.invalidate()

            }

       }

as you see in my codes I have another variable that controls the download finish so when progress is 1.0 I want to dismiss alert


回答1:


The Problem here is that you are presenting the alert view inside the 'downloadProgress' closure. This is not what its meant for.

The 'downloadProgress' closure basically gets called every time there has been a change in the progress. So when downloading a file this obviously can lead to many alert views layered on top of each other as you described.

Actually you should instantiate your Alert View somewhere outside and only use this closure to update it's content.

But if you want to keep this structure, test for progress.isFinished instead for a specific value.



来源:https://stackoverflow.com/questions/46137799/why-alamofire-downloads-progress-in-alert-will-flashing-and-the-behind-screen-st

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!