@@ -11,16 +11,56 @@ import Foundation
11
11
import FoundationNetworking
12
12
#endif
13
13
14
- class ParseURLSessionDelegate : NSObject , URLSessionDelegate , URLSessionDataDelegate , URLSessionDownloadDelegate
14
+ class ParseURLSessionDelegate : NSObject
15
15
{
16
16
var callbackQueue : DispatchQueue
17
17
var authentication : ( ( URLAuthenticationChallenge ,
18
18
( URLSession . AuthChallengeDisposition ,
19
19
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
20
60
var downloadDelegates = [ URLSessionDownloadTask : ( ( URLSessionDownloadTask , Int64 , Int64 , Int64 ) -> Void ) ] ( )
21
61
var uploadDelegates = [ URLSessionTask : ( ( URLSessionTask , Int64 , Int64 , Int64 ) -> Void ) ] ( )
22
- var streamDelegates = [ URLSessionTask: InputStream] ( )
23
62
var taskCallbackQueues = [ URLSessionTask: DispatchQueue] ( )
63
+ #endif
24
64
25
65
init ( callbackQueue: DispatchQueue ,
26
66
authentication: ( ( URLAuthenticationChallenge ,
@@ -30,7 +70,9 @@ class ParseURLSessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
30
70
self . authentication = authentication
31
71
super. init ( )
32
72
}
73
+ }
33
74
75
+ extension ParseURLSessionDelegate : URLSessionDelegate {
34
76
func urlSession( _ session: URLSession ,
35
77
didReceive challenge: URLAuthenticationChallenge ,
36
78
completionHandler: @escaping ( URLSession . AuthChallengeDisposition ,
@@ -43,60 +85,95 @@ class ParseURLSessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
43
85
completionHandler ( . performDefaultHandling, nil )
44
86
}
45
87
}
88
+ }
46
89
90
+ extension ParseURLSessionDelegate : URLSessionDataDelegate {
47
91
func urlSession( _ session: URLSession ,
48
92
task: URLSessionTask ,
49
93
didSendBodyData bytesSent: Int64 ,
50
94
totalBytesSent: Int64 ,
51
95
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] ,
53
107
let queue = taskCallbackQueues [ task] {
54
-
55
108
queue. async {
56
- callBack ( task, bytesSent, totalBytesSent, totalBytesExpectedToSend)
109
+ callback ( task, bytesSent, totalBytesSent, totalBytesExpectedToSend)
110
+ }
111
+ }
112
+ #endif
113
+ }
57
114
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)
61
130
}
62
131
}
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
63
139
}
140
+ }
64
141
142
+ extension ParseURLSessionDelegate : URLSessionDownloadDelegate {
65
143
func urlSession( _ session: URLSession ,
66
144
downloadTask: URLSessionDownloadTask ,
67
145
didWriteData bytesWritten: Int64 ,
68
146
totalBytesWritten: Int64 ,
69
147
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] ,
72
159
let queue = taskCallbackQueues [ downloadTask] {
73
160
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)
78
162
}
79
163
}
164
+ #endif
80
165
}
81
166
82
167
func urlSession( _ session: URLSession ,
83
168
downloadTask: URLSessionDownloadTask ,
84
169
didFinishDownloadingTo location: URL ) {
170
+ #if compiler(>=5.5.2) && canImport(_Concurrency)
171
+ Task {
172
+ await delegates. removeDownload ( downloadTask)
173
+ }
174
+ #else
85
175
downloadDelegates. removeValue ( forKey: downloadTask)
86
176
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
101
178
}
102
179
}
0 commit comments