@@ -18,6 +18,9 @@ class URLSessionUploader: NSObject {
1818
1919 var session: URLSession?
2020 let queue = OperationQueue()
21+
22+ // Accessing uploadedData & runningTaskById will require exclusive access
23+ private let semaphore = DispatchSemaphore(value: 1)
2124
2225 // Reference for uploaded data.
2326 var uploadedData = [String: Data]()
@@ -51,7 +54,10 @@ class URLSessionUploader: NSObject {
5154 delegates.uploadEnqueued(taskId: taskId)
5255
5356 uploadTask.resume()
57+
58+ semaphore.wait()
5459 self.runningTaskById[taskId] = UploadTask(taskId: taskId, status: .enqueue, progress: 0)
60+ semaphore.signal()
5561
5662 return uploadTask
5763 }
@@ -153,6 +159,11 @@ extension URLSessionUploader: URLSessionDelegate, URLSessionDataDelegate, URLSes
153159 }
154160
155161 func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
162+ semaphore.wait()
163+ defer {
164+ semaphore.signal()
165+ }
166+
156167 NSLog("URLSessionDidReceiveData:")
157168
158169 guard let uploadTask = dataTask as? URLSessionUploadTask else {
@@ -179,6 +190,11 @@ extension URLSessionUploader: URLSessionDelegate, URLSessionDataDelegate, URLSes
179190 }
180191
181192 public func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
193+ semaphore.wait()
194+ defer {
195+ semaphore.signal()
196+ }
197+
182198 if totalBytesExpectedToSend == NSURLSessionTransferSizeUnknown {
183199 NSLog("Unknown transfer size")
184200 } else {
@@ -191,6 +207,7 @@ extension URLSessionUploader: URLSessionDelegate, URLSessionDataDelegate, URLSes
191207 let bytesExpectedToSend = Double(integerLiteral: totalBytesExpectedToSend)
192208 let tBytesSent = Double(integerLiteral: totalBytesSent)
193209 let progress = round(Double(tBytesSent / bytesExpectedToSend * 100))
210+
194211 let runningTask = self.runningTaskById[taskId]
195212 NSLog("URLSessionDidSendBodyData: taskId: \(taskId), byteSent: \(bytesSent), totalBytesSent: \(totalBytesSent), totalBytesExpectedToSend: \(totalBytesExpectedToSend), progress:\(progress)")
196213
@@ -211,7 +228,13 @@ extension URLSessionUploader: URLSessionDelegate, URLSessionDataDelegate, URLSes
211228
212229 public func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
213230 NSLog("URLSessionDidFinishEvents:")
231+
214232 session.getTasksWithCompletionHandler { (_, uploadTasks, _) in
233+ self.semaphore.wait()
234+ defer {
235+ self.semaphore.signal()
236+ }
237+
215238 if uploadTasks.isEmpty {
216239 NSLog("all upload tasks have been completed")
217240
@@ -222,6 +245,11 @@ extension URLSessionUploader: URLSessionDelegate, URLSessionDataDelegate, URLSes
222245 }
223246
224247 public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
248+ semaphore.wait()
249+ defer {
250+ semaphore.signal()
251+ }
252+
225253 guard let uploadTask = task as? URLSessionUploadTask else {
226254 NSLog("URLSessionDidCompleteWithError: not an uplaod task")
227255 return
0 commit comments