Skip to content

Commit 12e60f6

Browse files
authored
Merge pull request #2587 from broadwaylamb/urlsession-fixes
Make URLSession and URLSessionTask more source-compatible with Darwin
2 parents beb667d + 8ca6c4d commit 12e60f6

File tree

2 files changed

+54
-61
lines changed

2 files changed

+54
-61
lines changed

Foundation/URLSession/URLSession.swift

Lines changed: 53 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ open class URLSession : NSObject {
268268
}
269269

270270
open private(set) var delegateQueue: OperationQueue
271-
open var delegate: URLSessionDelegate?
271+
open private(set) var delegate: URLSessionDelegate?
272272
open private(set) var configuration: URLSessionConfiguration
273273

274274
/*
@@ -370,8 +370,13 @@ open class URLSession : NSObject {
370370
}
371371
}
372372

373+
@available(*, unavailable, renamed: "getTasksWithCompletionHandler(_:)")
374+
open func getTasksWithCompletionHandler(completionHandler: @escaping ([URLSessionDataTask], [URLSessionUploadTask], [URLSessionDownloadTask]) -> Void) {
375+
getTasksWithCompletionHandler(completionHandler)
376+
}
377+
373378
/* invokes completionHandler with outstanding data, upload and download tasks. */
374-
open func getTasksWithCompletionHandler(completionHandler: @escaping ([URLSessionDataTask], [URLSessionUploadTask], [URLSessionDownloadTask]) -> Void) {
379+
open func getTasksWithCompletionHandler(_ completionHandler: @escaping ([URLSessionDataTask], [URLSessionUploadTask], [URLSessionDownloadTask]) -> Void) {
375380
workQueue.async {
376381
self.delegateQueue.addOperation {
377382
var dataTasks = [URLSessionDataTask]()
@@ -419,6 +424,22 @@ open class URLSession : NSObject {
419424
open func dataTask(with url: URL) -> URLSessionDataTask {
420425
return dataTask(with: _Request(url), behaviour: .callDelegate)
421426
}
427+
428+
/*
429+
* data task convenience methods. These methods create tasks that
430+
* bypass the normal delegate calls for response and data delivery,
431+
* and provide a simple cancelable asynchronous interface to receiving
432+
* data. Errors will be returned in the NSURLErrorDomain,
433+
* see <Foundation/NSURLError.h>. The delegate, if any, will still be
434+
* called for authentication challenges.
435+
*/
436+
open func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
437+
return dataTask(with: _Request(request), behaviour: .dataCompletionHandler(completionHandler))
438+
}
439+
440+
open func dataTask(with url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
441+
return dataTask(with: _Request(url), behaviour: .dataCompletionHandler(completionHandler))
442+
}
422443

423444
/* Creates an upload task with the given request. The body of the request will be created from the file referenced by fileURL */
424445
open func uploadTask(with request: URLRequest, fromFile fileURL: URL) -> URLSessionUploadTask {
@@ -437,6 +458,18 @@ open class URLSession : NSObject {
437458
let r = URLSession._Request(request)
438459
return uploadTask(with: r, body: nil, behaviour: .callDelegate)
439460
}
461+
462+
/*
463+
* upload convenience method.
464+
*/
465+
open func uploadTask(with request: URLRequest, fromFile fileURL: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionUploadTask {
466+
let fileData = try! Data(contentsOf: fileURL)
467+
return uploadTask(with: request, from: fileData, completionHandler: completionHandler)
468+
}
469+
470+
open func uploadTask(with request: URLRequest, from bodyData: Data?, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionUploadTask {
471+
return uploadTask(with: _Request(request), body: .data(createDispatchData(bodyData!)), behaviour: .dataCompletionHandler(completionHandler))
472+
}
440473

441474
/* Creates a download task with the given request. */
442475
open func downloadTask(with request: URLRequest) -> URLSessionDownloadTask {
@@ -453,6 +486,24 @@ open class URLSession : NSObject {
453486
open func downloadTask(withResumeData resumeData: Data) -> URLSessionDownloadTask {
454487
return invalidDownloadTask(behavior: .callDelegate)
455488
}
489+
490+
/*
491+
* download task convenience methods. When a download successfully
492+
* completes, the URL will point to a file that must be read or
493+
* copied during the invocation of the completion routine. The file
494+
* will be removed automatically.
495+
*/
496+
open func downloadTask(with request: URLRequest, completionHandler: @escaping (URL?, URLResponse?, Error?) -> Void) -> URLSessionDownloadTask {
497+
return downloadTask(with: _Request(request), behavior: .downloadCompletionHandler(completionHandler))
498+
}
499+
500+
open func downloadTask(with url: URL, completionHandler: @escaping (URL?, URLResponse?, Error?) -> Void) -> URLSessionDownloadTask {
501+
return downloadTask(with: _Request(url), behavior: .downloadCompletionHandler(completionHandler))
502+
}
503+
504+
open func downloadTask(withResumeData resumeData: Data, completionHandler: @escaping (URL?, URLResponse?, Error?) -> Void) -> URLSessionDownloadTask {
505+
return invalidDownloadTask(behavior: .downloadCompletionHandler(completionHandler))
506+
}
456507

457508
/* Creates a bidirectional stream task to a given host and port.
458509
*/
@@ -556,64 +607,6 @@ fileprivate extension URLSession {
556607
}
557608
}
558609

559-
560-
/*
561-
* URLSession convenience routines deliver results to
562-
* a completion handler block. These convenience routines
563-
* are not available to URLSessions that are configured
564-
* as background sessions.
565-
*
566-
* Task objects are always created in a suspended state and
567-
* must be sent the -resume message before they execute.
568-
*/
569-
extension URLSession {
570-
/*
571-
* data task convenience methods. These methods create tasks that
572-
* bypass the normal delegate calls for response and data delivery,
573-
* and provide a simple cancelable asynchronous interface to receiving
574-
* data. Errors will be returned in the NSURLErrorDomain,
575-
* see <Foundation/NSURLError.h>. The delegate, if any, will still be
576-
* called for authentication challenges.
577-
*/
578-
open func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
579-
return dataTask(with: _Request(request), behaviour: .dataCompletionHandler(completionHandler))
580-
}
581-
582-
open func dataTask(with url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
583-
return dataTask(with: _Request(url), behaviour: .dataCompletionHandler(completionHandler))
584-
}
585-
586-
/*
587-
* upload convenience method.
588-
*/
589-
open func uploadTask(with request: URLRequest, fromFile fileURL: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionUploadTask {
590-
let fileData = try! Data(contentsOf: fileURL)
591-
return uploadTask(with: request, from: fileData, completionHandler: completionHandler)
592-
}
593-
594-
open func uploadTask(with request: URLRequest, from bodyData: Data?, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionUploadTask {
595-
return uploadTask(with: _Request(request), body: .data(createDispatchData(bodyData!)), behaviour: .dataCompletionHandler(completionHandler))
596-
}
597-
598-
/*
599-
* download task convenience methods. When a download successfully
600-
* completes, the URL will point to a file that must be read or
601-
* copied during the invocation of the completion routine. The file
602-
* will be removed automatically.
603-
*/
604-
open func downloadTask(with request: URLRequest, completionHandler: @escaping (URL?, URLResponse?, Error?) -> Void) -> URLSessionDownloadTask {
605-
return downloadTask(with: _Request(request), behavior: .downloadCompletionHandler(completionHandler))
606-
}
607-
608-
open func downloadTask(with url: URL, completionHandler: @escaping (URL?, URLResponse?, Error?) -> Void) -> URLSessionDownloadTask {
609-
return downloadTask(with: _Request(url), behavior: .downloadCompletionHandler(completionHandler))
610-
}
611-
612-
open func downloadTask(withResumeData resumeData: Data, completionHandler: @escaping (URL?, URLResponse?, Error?) -> Void) -> URLSessionDownloadTask {
613-
return invalidDownloadTask(behavior: .downloadCompletionHandler(completionHandler))
614-
}
615-
}
616-
617610
internal extension URLSession {
618611
/// The kind of callback / delegate behaviour of a task.
619612
///

Foundation/URLSession/URLSessionTask.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ open class URLSessionTask : NSObject, NSCopying {
376376
/*
377377
* The current state of the task within the session.
378378
*/
379-
open var state: URLSessionTask.State {
379+
open fileprivate(set) var state: URLSessionTask.State {
380380
get {
381381
return self.syncQ.sync { self._state }
382382
}

0 commit comments

Comments
 (0)