0
我在本地主機上有一個普通的操作系統和一個簡單的TCP服務器。我想要做的就是取消已經有一個readData操作入隊的URLSessionStreamTask。真正瘋狂的是,這個精確的操場對於iOS或tvOS來說工作得很好,但對MacOS來說卻不是。取消URLSessionStreamTask永遠不會使用NSURLErrorCancelled(在macOS上)調用completionHandler
在iOS/tvOS我得到以下輸出: 「勝利」
Resuming...
Cancelling...
After cancel call
Victory! The session task was properly cancelled!
在MacOS(10.11)的消息永遠不會被打印。
這裏是操場:
import Foundation
import PlaygroundSupport
let task = URLSession.shared.streamTask(withHostName: "localhost", port: 12345)
task.readData(ofMinLength: 1, maxLength: 1024, timeout: 0) { (data, atEOF, error) in
if let error = error as? NSError {
if error.code == NSURLErrorCancelled {
print("Victory! The session task was properly cancelled!")
}
}
}
print("Resuming...")
task.resume()
print("Cancelling...")
task.cancel()
print("After cancel call")
PlaygroundPage.current.needsIndefiniteExecution = true
我在這裏失去了一些東西?我正在使用XCode 8(測試版4)。任何幫助深表感謝。謝謝。
這可能是一個CPU速度問題。具體來說,會話可能已經從流中讀取所有數據並將其存儲在本地,等待您的應用程序請求它,然後您的應用程序甚至有時間取消連接。如果您的服務器發送......說一千兆字節的數據,是否也會出現此問題? – dgatwood