2017-01-26 2 views
1

내가 NSProgressIndicator (진행률 표시 줄)가 함께 함수 내에서 작동하지 : 콜백과 함수 내부에 위치NSProgressIndicator는 같은 대리자 메서드 내에서 때 완벽하게 작동하는 것 같다 콜백

func restClient(client: DBRestClient!, uploadProgress progress: CGFloat, forFile destPath: String!, from srcPath: String!) { 
     if progress == 1.0 { 
      progressBar.hidden = true 
      generalAlert("Uploaded", text: "Your File has been saved to your computer and uploaded under: \(destPath)") 
     } else { 
      progressBar.hidden = false 
      print(progress) // e.g 0.010213 
      progressBar.doubleValue = Double(progress)*100 
     } 
    } 
그러나

, I 어떤 식 으로든 그것을 수정할 수있는 것 같지 않습니다 (내 isHidden = false 호출도 doubleValue 업데이트도 허용하지 않습니다).

CloudConvert.convert([ 
      "inputformat": "gif", 
      "outputformat" : "mov", 
      "input" : "upload", 
      "file": name], 

      progressHandler: { (step, percent, message) -> Void in 
       print(percentage!) //e.g 10.31451. Called every second 
       self.progressBar.hidden = false //Doesn't Work 
       self.progressBar.doubleValue = Double(percentage!) //Doesn't Work 
      }, 
      completionHandler: { (path, error) -> Void in 
       if(error != nil) { 
        self.progressBar.hidden = true 
        self.generalAlert("ERROR", text: "Your file could not be uploaded") 
       } else { 
        self.progressBar.hidden = true 
        self.generalAlert("MOV Uploaded", text: "Your MOV has been saved to your computer and uploaded") 
       } 
     }) 

아무도 왜 이런 생각 일 수 있습니까?

+0

콜백이 주 스레드에 있는지 확인 하시겠습니까? –

+0

나는 아니었다! 그 대답이었다! 당신이 그것을 받아 들일 수 있도록 대답이 아닌 주석으로 써 주시겠습니까? –

답변

1

완성 처리기는 백그라운드 스레드에서 모든 것을 실행하며 기본 스레드에서 UI 호출을해야합니다.

는 DispatchQueue.main.async 블록에 UI 코드를 넣어이

DispatchQueue.main.async(execute: { 
     self.progressBar.hidden = false //Should now Work 
     self.progressBar.doubleValue = Double(percentage!) //Should now Work 
    }) 
+0

감사합니다. 질문이 관련성이 있다고 판단되면 upvote하십시오. –

1

시도와 같은 주요 큐에 파견에서 진행 표시 줄 호출을 감싸. UI 작업 메인 스레드가 꺼지면 지연이 발생합니다.