I'm attempting to test the cancellation of a DispatchWork
item within a Swift Playground, although during the first few milliseconds of execution there's an error, which I'm not sure what it's actually indicating, nor can I tell if the error is causing the cancellation rather than the cancel()
method...
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.}
Has someone got an idea what that error is indicating?
Set needsIndefiniteExecution
to true
can omit this warning. The warning happens once playground execution ends earlier than thread processing.
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With