2016-07-11 2 views
1

때때로 비동기 스레드의 오류 경고를 표시하는 함수를 호출하는 IBAction이 있습니다. 이 시뮬레이터에서 "작품"하지만이 오류가있어 :이 오류가 표시되면Swift 3.0의 비동기 스레드에서 UIAlertController 호출

CoreAnimation: warning, deleted thread with uncommitted CATransaction; set CA_DEBUG_TRANSACTIONS=1 in environment to log backtraces.

을, 나는 메인 스레드 해제 UI를 업데이트하려고하고 있었고, 난 그것을 수정해야 깨달았다.

여기에 내가 전화 한 비동기 함수의 :

let queue = DispatchQueue(label: "com.adrianbindc.myApp.myFunction") 

queue.async { 
    self.createArray() 

    switch self.arrayIsEmpty() { 
    case true: 
     // TODO: update on the main thread 
     self.displayAlert(alertTitle: "Error Title", alertMessage: "Error Message") 
    case false: 

     // Do Stuff Off the Main Queue 
     DispatchQueue.main.async{ 
      switch someArray.count { 
      case 0: 
       // TODO: update on the main thread 
       self.displayAlert(alertTitle: "Another Error Title", alertMessage: "Another error message.") 
      default: 
       // Do other stuff 
      } 
     }  
    } 
} 

/** 
Utility method to display a single alert with an OK button. 
*/ 
func displayAlert(alertTitle: String, alertMessage: String) { 
    let alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert) 
    let okAction = UIAlertAction(title: "OK", style: .default, handler: nil) 
    alertController.addAction(okAction) 
    self.present(alertController, animated: false, completion: nil) 
} 

나는 내 displayAlert 기능의 앞에 @objc을 추가하고 내에서이 같은 경고 함수를 호출 시도

시도했다 비동기 블록 :

self.performSelector(onMainThread: #selector(ViewController.displayAlert(alertTitle: "Error Title", alertMessage: "Error Message")), with: self, waitUntilDone: false) 

하지만 컴파일러에서이 오류가 발생합니다.

Use of instance member 'displayAlert' on type 'ViewController'; did you mean to use a value of type 'ViewController' instead?

내 실수가 많은 부분에 대한 의견을 듣고 싶습니다. 읽어 주셔서 감사합니다.

+0

'performSelector' 대신'DispatchQueue.main.async {displayAlert (alertTitle : "오류 제목", alertMessage : "오류 메시지")}}를 사용합니다. –

+1

@AaronBrager 감사합니다. 그게 내가 끝내 준거야. 방금 내 답변을 게시 :) – Adrian

답변

4

나는 올바른 길을 가고 있었지만 잘못된 구문을 사용하고있었습니다 (평소와 같이). alert 함수를 호출하면 주 스레드에서 처리해야하므로 주 스레드가 필요합니다.

다음
// ** This gets the main queue ** 
DispatchQueue.main.async(execute: { 
    self.displayAlert(alertTitle: "Another Error Title", alertMessage: "Another error message.") 
} 

이 완성 된 제품의 모습입니다 : 여기

내가 비동기 블록 내 경고라는 방법

let queue = DispatchQueue(label: "com.adrianbindc.myApp.myFunction") 

queue.async { 
    self.createArray() 

    switch self.arrayIsEmpty() { 
    case true: 
     // ** This gets the main queue ** 
     DispatchQueue.main.async(execute: { 
      self.displayAlert(alertTitle: "Error Title", alertMessage: "error message.") 
     }) 

    case false: 
     switch resultArray.count { 
     case 0: 
      // ** This gets the main queue ** 
      DispatchQueue.main.async(execute: { 
       self.displayAlert(alertTitle: "Another Error Title", alertMessage: "Another error message.") 
      }) 
     default: 
      // Do other stuff 
     } 
    } 
} 

This answer

가 결승선을 통해 저를 가지고 더 많은 시나리오를 갖고있는 몇 월 Swift 3.0에 도움이됩니다.