Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode: Error Domain=DVTPlaygroundCommunicationErrorDomain Code=1

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?

like image 801
l'L'l Avatar asked Oct 26 '16 18:10

l'L'l


1 Answers

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
like image 167
Allen Avatar answered Oct 19 '22 23:10

Allen