2016-02-02 18 views
0

在我的簡單代碼中,如果條件x == 2並且轉到主隊列,我希望在隊列全局中斷進程。dispatch_async中的break隊列處理iOS Swift 2.0

如何繼續?

達里奧·巴索卡多佐

var x = 1 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW,0)) { 
    x++ 

    if x == 2 { 
     break // error in this part or 
     return // does not work well 
    } 

    // more codes... 
    dispatch_async(dispatch_get_main_queue()) { () -> Void in 
     // execute the main code 
     // with break or not 
     if x == 2 { 
      // verified that break 
     } 

     // next codes 
    } 
} 

回答

0

沒有必要明確地退出你的過程中,一旦程序到達您的異步過程結束時它會自行退出。

你需要反過來想一想。只要有東西需要計算,請保持這個過程,然後讓它耗盡

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW,0))  { 
    while x < 2 { 
    x++ 
    } 

    dispatch_async(dispatch_get_main_queue()) { () -> Void in { 
    ... 
    } 
}