@@ -268,7 +268,7 @@ open class URLSession : NSObject {
268
268
}
269
269
270
270
open private( set) var delegateQueue : OperationQueue
271
- open var delegate : URLSessionDelegate ?
271
+ open private ( set ) var delegate : URLSessionDelegate ?
272
272
open private( set) var configuration : URLSessionConfiguration
273
273
274
274
/*
@@ -370,8 +370,13 @@ open class URLSession : NSObject {
370
370
}
371
371
}
372
372
373
+ @available ( * , unavailable, renamed: " getTasksWithCompletionHandler(_:) " )
374
+ open func getTasksWithCompletionHandler( completionHandler: @escaping ( [ URLSessionDataTask ] , [ URLSessionUploadTask ] , [ URLSessionDownloadTask ] ) -> Void ) {
375
+ getTasksWithCompletionHandler ( completionHandler)
376
+ }
377
+
373
378
/* 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 ) {
375
380
workQueue. async {
376
381
self . delegateQueue. addOperation {
377
382
var dataTasks = [ URLSessionDataTask] ( )
@@ -419,6 +424,22 @@ open class URLSession : NSObject {
419
424
open func dataTask( with url: URL ) -> URLSessionDataTask {
420
425
return dataTask ( with: _Request ( url) , behaviour: . callDelegate)
421
426
}
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
+ }
422
443
423
444
/* Creates an upload task with the given request. The body of the request will be created from the file referenced by fileURL */
424
445
open func uploadTask( with request: URLRequest , fromFile fileURL: URL ) -> URLSessionUploadTask {
@@ -437,6 +458,18 @@ open class URLSession : NSObject {
437
458
let r = URLSession . _Request ( request)
438
459
return uploadTask ( with: r, body: nil , behaviour: . callDelegate)
439
460
}
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
+ }
440
473
441
474
/* Creates a download task with the given request. */
442
475
open func downloadTask( with request: URLRequest ) -> URLSessionDownloadTask {
@@ -453,6 +486,24 @@ open class URLSession : NSObject {
453
486
open func downloadTask( withResumeData resumeData: Data ) -> URLSessionDownloadTask {
454
487
return invalidDownloadTask ( behavior: . callDelegate)
455
488
}
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
+ }
456
507
457
508
/* Creates a bidirectional stream task to a given host and port.
458
509
*/
@@ -556,64 +607,6 @@ fileprivate extension URLSession {
556
607
}
557
608
}
558
609
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
-
617
610
internal extension URLSession {
618
611
/// The kind of callback / delegate behaviour of a task.
619
612
///
0 commit comments