Skip to content

Commit 7b3c205

Browse files
committed
Option 3?
1 parent d3b7f9c commit 7b3c205

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

Foundation/URLSession/URLSessionTask.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ open class URLSessionTask : NSObject, NSCopying {
3333
internal var suspendCount = 1
3434
internal var session: URLSessionProtocol! //change to nil when task completes
3535
internal let body: _Body
36-
fileprivate var _protocol: URLProtocol! = nil
36+
fileprivate var _protocol: URLProtocol? = nil
3737
private let syncQ = DispatchQueue(label: "org.swift.URLSessionTask.SyncQ")
3838

3939
/// All operations must run on this queue.
@@ -88,6 +88,7 @@ open class URLSessionTask : NSObject, NSCopying {
8888
self._protocol = urlProtocol.init(task: self, cachedResponse: nil, client: nil)
8989
}
9090
}
91+
guard self._protocol != nil else { fatalError("Unable to find protocol handler for \(request)") }
9192
}
9293
deinit {
9394
//TODO: Do we remove the EasyHandle from the session here? This might run on the wrong thread / queue.
@@ -178,8 +179,8 @@ open class URLSessionTask : NSObject, NSCopying {
178179
self.workQueue.async {
179180
let urlError = URLError(_nsError: NSError(domain: NSURLErrorDomain, code: NSURLErrorCancelled, userInfo: nil))
180181
self.error = urlError
181-
self._protocol.stopLoading()
182-
self._protocol.client?.urlProtocol(self._protocol, didFailWithError: urlError)
182+
self._protocol?.stopLoading()
183+
self._protocol?.client?.urlProtocol(self._protocol!, didFailWithError: urlError)
183184
}
184185
}
185186
}
@@ -235,7 +236,7 @@ open class URLSessionTask : NSObject, NSCopying {
235236

236237
if self.suspendCount == 1 {
237238
self.workQueue.async {
238-
self._protocol.stopLoading()
239+
self._protocol?.stopLoading()
239240
}
240241
}
241242
}
@@ -250,7 +251,7 @@ open class URLSessionTask : NSObject, NSCopying {
250251
self.updateTaskState()
251252
if self.suspendCount == 0 {
252253
self.workQueue.async {
253-
self._protocol.startLoading()
254+
self._protocol?.startLoading()
254255
}
255256
}
256257
}

0 commit comments

Comments
 (0)