Skip to content

Commit 5a0e57d

Browse files
authored
Merge pull request #983 from mamabusi/urlSesSerialDelegQueue
2 parents 3ab67ad + 6f7ea2c commit 5a0e57d

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Foundation/NSURLSession/NSURLSession.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ open class URLSession : NSObject {
211211
self.workQueue = DispatchQueue(label: "URLSession<\(identifier)>")
212212
self.taskAttributesIsolation = DispatchQueue(label: "URLSession<\(identifier)>.taskAttributes", attributes: DispatchQueue.Attributes.concurrent)
213213
self.delegateQueue = OperationQueue()
214+
self.delegateQueue.maxConcurrentOperationCount = 1
214215
self.delegate = nil
215216
//TODO: Make sure this one can't be written to?
216217
// Could create a subclass of URLSessionConfiguration that wraps the
@@ -220,12 +221,22 @@ open class URLSession : NSObject {
220221
self._configuration = c
221222
self.multiHandle = _MultiHandle(configuration: c, workQueue: workQueue)
222223
}
224+
225+
/*
226+
* A delegate queue should be serial to ensure correct ordering of callbacks.
227+
* However, if user supplies a concurrent delegateQueue it is not converted to serial.
228+
*/
223229
public /*not inherited*/ init(configuration: URLSessionConfiguration, delegate: URLSessionDelegate?, delegateQueue queue: OperationQueue?) {
224230
initializeLibcurl()
225231
identifier = nextSessionIdentifier()
226232
self.workQueue = DispatchQueue(label: "URLSession<\(identifier)>")
227233
self.taskAttributesIsolation = DispatchQueue(label: "URLSession<\(identifier)>.taskAttributes", attributes: DispatchQueue.Attributes.concurrent)
228-
self.delegateQueue = queue ?? OperationQueue()
234+
if let _queue = queue {
235+
self.delegateQueue = _queue
236+
} else {
237+
self.delegateQueue = OperationQueue()
238+
self.delegateQueue.maxConcurrentOperationCount = 1
239+
}
229240
self.delegate = delegate
230241
//TODO: Make sure this one can't be written to?
231242
// Could create a subclass of URLSessionConfiguration that wraps the

0 commit comments

Comments
 (0)