@@ -11,16 +11,56 @@ import Foundation
1111import FoundationNetworking
1212#endif
1313
14- class ParseURLSessionDelegate : NSObject , URLSessionDelegate , URLSessionDataDelegate , URLSessionDownloadDelegate
14+ class ParseURLSessionDelegate : NSObject
1515{
1616 var callbackQueue : DispatchQueue
1717 var authentication : ( ( URLAuthenticationChallenge ,
1818 ( URLSession . AuthChallengeDisposition ,
1919 URLCredential ? ) -> Void ) -> Void ) ?
20+ var streamDelegates = [ URLSessionTask: InputStream] ( )
21+ #if compiler(>=5.5.2) && canImport(_Concurrency)
22+ actor SessionDelegate : Sendable {
23+ var downloadDelegates = [ URLSessionDownloadTask : ( ( URLSessionDownloadTask , Int64 , Int64 , Int64 ) -> Void ) ] ( )
24+ var uploadDelegates = [ URLSessionTask : ( ( URLSessionTask , Int64 , Int64 , Int64 ) -> Void ) ] ( )
25+ var taskCallbackQueues = [ URLSessionTask: DispatchQueue] ( )
26+
27+ func updateDownload( _ task: URLSessionDownloadTask ,
28+ callback: ( ( URLSessionDownloadTask , Int64 , Int64 , Int64 ) -> Void ) ? ) {
29+ downloadDelegates [ task] = callback
30+ }
31+
32+ func removeDownload( _ task: URLSessionDownloadTask ) {
33+ downloadDelegates. removeValue ( forKey: task)
34+ taskCallbackQueues. removeValue ( forKey: task)
35+ }
36+
37+ func updateUpload( _ task: URLSessionTask ,
38+ callback: ( ( URLSessionTask , Int64 , Int64 , Int64 ) -> Void ) ? ) {
39+ uploadDelegates [ task] = callback
40+ }
41+
42+ func removeUpload( _ task: URLSessionTask ) {
43+ uploadDelegates. removeValue ( forKey: task)
44+ taskCallbackQueues. removeValue ( forKey: task)
45+ }
46+
47+ func updateTask( _ task: URLSessionTask ,
48+ queue: DispatchQueue ) {
49+ taskCallbackQueues [ task] = queue
50+ }
51+
52+ func removeTask( _ task: URLSessionTask ) {
53+ taskCallbackQueues. removeValue ( forKey: task)
54+ }
55+ }
56+
57+ var delegates = SessionDelegate ( )
58+
59+ #else
2060 var downloadDelegates = [ URLSessionDownloadTask : ( ( URLSessionDownloadTask , Int64 , Int64 , Int64 ) -> Void ) ] ( )
2161 var uploadDelegates = [ URLSessionTask : ( ( URLSessionTask , Int64 , Int64 , Int64 ) -> Void ) ] ( )
22- var streamDelegates = [ URLSessionTask: InputStream] ( )
2362 var taskCallbackQueues = [ URLSessionTask: DispatchQueue] ( )
63+ #endif
2464
2565 init ( callbackQueue: DispatchQueue ,
2666 authentication: ( ( URLAuthenticationChallenge ,
@@ -30,7 +70,9 @@ class ParseURLSessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
3070 self . authentication = authentication
3171 super. init ( )
3272 }
73+ }
3374
75+ extension ParseURLSessionDelegate : URLSessionDelegate {
3476 func urlSession( _ session: URLSession ,
3577 didReceive challenge: URLAuthenticationChallenge ,
3678 completionHandler: @escaping ( URLSession . AuthChallengeDisposition ,
@@ -43,60 +85,95 @@ class ParseURLSessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
4385 completionHandler ( . performDefaultHandling, nil )
4486 }
4587 }
88+ }
4689
90+ extension ParseURLSessionDelegate : URLSessionDataDelegate {
4791 func urlSession( _ session: URLSession ,
4892 task: URLSessionTask ,
4993 didSendBodyData bytesSent: Int64 ,
5094 totalBytesSent: Int64 ,
5195 totalBytesExpectedToSend: Int64 ) {
52- if let callBack = uploadDelegates [ task] ,
96+ #if compiler(>=5.5.2) && canImport(_Concurrency)
97+ Task {
98+ if let callback = await delegates. uploadDelegates [ task] ,
99+ let queue = await delegates. taskCallbackQueues [ task] {
100+ queue. async {
101+ callback ( task, bytesSent, totalBytesSent, totalBytesExpectedToSend)
102+ }
103+ }
104+ }
105+ #else
106+ if let callback = uploadDelegates [ task] ,
53107 let queue = taskCallbackQueues [ task] {
54-
55108 queue. async {
56- callBack ( task, bytesSent, totalBytesSent, totalBytesExpectedToSend)
109+ callback ( task, bytesSent, totalBytesSent, totalBytesExpectedToSend)
110+ }
111+ }
112+ #endif
113+ }
57114
58- if totalBytesSent == totalBytesExpectedToSend {
59- self . uploadDelegates. removeValue ( forKey: task)
60- }
115+ func urlSession( _ session: URLSession ,
116+ task: URLSessionTask ,
117+ needNewBodyStream completionHandler: @escaping ( InputStream ? ) -> Void ) {
118+ if let stream = streamDelegates [ task] {
119+ completionHandler ( stream)
120+ }
121+ }
122+
123+ func urlSession( _ session: URLSession , task: URLSessionTask , didCompleteWithError error: Error ? ) {
124+ streamDelegates. removeValue ( forKey: task)
125+ #if compiler(>=5.5.2) && canImport(_Concurrency)
126+ Task {
127+ await delegates. removeUpload ( task)
128+ if let downloadTask = task as? URLSessionDownloadTask {
129+ await delegates. removeDownload ( downloadTask)
61130 }
62131 }
132+ #else
133+ uploadDelegates. removeValue ( forKey: task)
134+ taskCallbackQueues. removeValue ( forKey: task)
135+ if let downloadTask = task as? URLSessionDownloadTask {
136+ downloadDelegates. removeValue ( forKey: downloadTask)
137+ }
138+ #endif
63139 }
140+ }
64141
142+ extension ParseURLSessionDelegate : URLSessionDownloadDelegate {
65143 func urlSession( _ session: URLSession ,
66144 downloadTask: URLSessionDownloadTask ,
67145 didWriteData bytesWritten: Int64 ,
68146 totalBytesWritten: Int64 ,
69147 totalBytesExpectedToWrite: Int64 ) {
70-
71- if let callBack = downloadDelegates [ downloadTask] ,
148+ #if compiler(>=5.5.2) && canImport(_Concurrency)
149+ Task {
150+ if let callback = await delegates. downloadDelegates [ downloadTask] ,
151+ let queue = await delegates. taskCallbackQueues [ downloadTask] {
152+ queue. async {
153+ callback ( downloadTask, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite)
154+ }
155+ }
156+ }
157+ #else
158+ if let callback = downloadDelegates [ downloadTask] ,
72159 let queue = taskCallbackQueues [ downloadTask] {
73160 queue. async {
74- callBack ( downloadTask, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite)
75- if totalBytesWritten == totalBytesExpectedToWrite {
76- self . downloadDelegates. removeValue ( forKey: downloadTask)
77- }
161+ callback ( downloadTask, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite)
78162 }
79163 }
164+ #endif
80165 }
81166
82167 func urlSession( _ session: URLSession ,
83168 downloadTask: URLSessionDownloadTask ,
84169 didFinishDownloadingTo location: URL ) {
170+ #if compiler(>=5.5.2) && canImport(_Concurrency)
171+ Task {
172+ await delegates. removeDownload ( downloadTask)
173+ }
174+ #else
85175 downloadDelegates. removeValue ( forKey: downloadTask)
86176 taskCallbackQueues. removeValue ( forKey: downloadTask)
87- }
88-
89- func urlSession( _ session: URLSession ,
90- task: URLSessionTask ,
91- needNewBodyStream completionHandler: @escaping ( InputStream ? ) -> Void ) {
92- if let stream = streamDelegates [ task] {
93- completionHandler ( stream)
94- }
95- }
96-
97- func urlSession( _ session: URLSession , task: URLSessionTask , didCompleteWithError error: Error ? ) {
98- uploadDelegates. removeValue ( forKey: task)
99- streamDelegates. removeValue ( forKey: task)
100- taskCallbackQueues. removeValue ( forKey: task)
177+ #endif
101178 }
102179}
0 commit comments