2016-10-26 37 views
5

我試圖在Swift Playground中測試DispatchWork項目的取消,雖然在執行的前幾個毫秒內有一個錯誤,我不確定它實際上是什麼說明,我也不能告訴我們,如果錯誤導致取消而非cancel()方法...Xcode:錯誤域= DVTPlaygroundCommunicationErrorDomain代碼= 1

func testDispatchWorkItems() { 
    let queue = DispatchQueue.global(qos: .userInitiated) 
    var item: DispatchWorkItem? 

    // create work item 

    item = DispatchWorkItem { 
     for i in 0 ... 100000 { 
      if item!.isCancelled { break } 
      print(i) 
     } 
    } 

    // start it 

    queue.async(execute: item!) 

    // after three seconds, stop it 

    queue.asyncAfter(deadline: .now() + 3) { 
     item?.cancel() 
    } 
} 

testDispatchWorkItems() 
2016-10-26 11:14:33.898 
com.apple.dt.Xcode.PlaygroundStub-macosx[30685:18567692] Error 
encountered communicating with Xcode: Error 
Domain=DVTPlaygroundCommunicationErrorDomain Code=1 "Cannot send data 
because stream is closed." UserInfo={NSLocalizedDescription=Cannot 
send data because stream is closed.} 

已經有人有一個想法是什麼錯誤指示?

+0

我發現了同樣的問題。它只發生在遊樂場 - 如果你在應用程序中運行它,它應該工作。開放以瞭解爲什麼如果有人知道,在操場上有這種限制。 –

回答

3

設置needsIndefiniteExecutiontrue可以省略此警告。一旦遊樂場執行結束的時間早於線程處理,警告就會發生

import PlaygroundSupport 
PlaygroundPage.current.needsIndefiniteExecution = true 
相關問題