2016-07-11 24 views
1

IBAction私は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

私は正しいパスにいたが、間違った構文(通常)を使用していた。私がアラート機能を呼び出すと、それはメインスレッド上のものです。つまり、メインスレッドを取得する必要があります。ここで

は、私は、非同期ブロック内のアラートと呼ばれる方法は次のとおりです。

// ** 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の参考にしてください。

関連する問題