2017-09-10 15 views
1

alamofire를 사용하여 다운로드 할 때 진행 상태가 경고로 표시되지만 문제는 진행률보기를 다운로드 할 때 경고가 완료되지만 깜박이고 다른 문제는 화면이 희미 해지기 시작한다는 것입니다. 블랙 나는 매 초마다 진행 상태보기가있는 새로운 알림이 표시 될 것이며 결국 알림을 많이 받는다는 것을 어떻게 생각합니까? 당신이 내 코드에서 보는 바와 같이경고에서 alamofire 다운로드 진행률이 왜 깜박이고 후위 화면이 검은 색으로 희미 해지기 시작합니까?

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) 
     } 

여기에 내가 다운로드 마무리를 제어하는 ​​또 다른 변수가 너무 진행 내가 경고를 해제 할 1.0 때 타이머

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() 

      } 

     } 

에 대한 코드입니다

답변

0

여기서 문제는 'downloadProgress'클로저 안에 경고보기를 표시한다는 것입니다. 이것은 그것이 의미했던 것이 아닙니다.

'downloadProgress'클로저는 기본적으로 진행 상황이 변경 될 때마다 호출됩니다. 파일을 다운로드 할 때 이것은 분명히 설명 된대로 여러 경고보기를 서로 위에 쌓을 수 있습니다.

실제로는 경고보기를 바깥 쪽에서 인스턴스화하고이 클로저를 사용하여 내용을 업데이트해야합니다.

그러나이 구조를 유지하려면 특정 값 대신 progress.isFinished을 테스트하십시오.