@@ -33,7 +33,7 @@ open class URLSessionTask : NSObject, NSCopying {
33
33
internal var suspendCount = 1
34
34
internal var session : URLSessionProtocol ! //change to nil when task completes
35
35
internal let body : _Body
36
- fileprivate var _protocol : URLProtocol ! = nil
36
+ fileprivate var _protocol : URLProtocol ? = nil
37
37
private let syncQ = DispatchQueue ( label: " org.swift.URLSessionTask.SyncQ " )
38
38
39
39
/// All operations must run on this queue.
@@ -88,6 +88,7 @@ open class URLSessionTask : NSObject, NSCopying {
88
88
self . _protocol = urlProtocol. init ( task: self , cachedResponse: nil , client: nil )
89
89
}
90
90
}
91
+ guard self . _protocol != nil else { fatalError ( " Unable to find protocol handler for \( request) " ) }
91
92
}
92
93
deinit {
93
94
//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 {
178
179
self . workQueue. async {
179
180
let urlError = URLError ( _nsError: NSError ( domain: NSURLErrorDomain, code: NSURLErrorCancelled, userInfo: nil ) )
180
181
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)
183
184
}
184
185
}
185
186
}
@@ -235,7 +236,7 @@ open class URLSessionTask : NSObject, NSCopying {
235
236
236
237
if self . suspendCount == 1 {
237
238
self . workQueue. async {
238
- self . _protocol. stopLoading ( )
239
+ self . _protocol? . stopLoading ( )
239
240
}
240
241
}
241
242
}
@@ -250,7 +251,7 @@ open class URLSessionTask : NSObject, NSCopying {
250
251
self . updateTaskState ( )
251
252
if self . suspendCount == 0 {
252
253
self . workQueue. async {
253
- self . _protocol. startLoading ( )
254
+ self . _protocol? . startLoading ( )
254
255
}
255
256
}
256
257
}
0 commit comments