Xcode:错误域= DVTPlaygroundCommunicationErrorDomain代码= 1

Xcode:错误域= DVTPlaygroundCommunicationErrorDomain代码= 1

问题描述:

我正在尝试测试Swift Playground中DispatchWork项的取消,尽管在执行的最初几毫秒中有一个错误,我不确定它实际上指示了什么,也无法确定错误导致取消,而不是cancel()方法...

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?

needsIndefiniteExecution设置为true可以忽略此警告.一旦游乐场执行比线程处理更早结束,就会发生警告.

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