Skip to content

Commit f6e7c03

Browse files
committed
Store and append data received during task
1 parent dc01c36 commit f6e7c03

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

Sources/TUSKit/TUSAPI.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ final class TUSAPI {
6666
private let queue = DispatchQueue(label: "com.tus.TUSAPI")
6767
private var backgroundHandler: (() -> Void)? = nil
6868
private var callbacks: [String: (Result<(Data?, HTTPURLResponse), Error>) -> Void] = [:]
69+
private var taskData: [String: Data] = [:]
6970

7071
deinit {
7172
if session.delegate is SessionDataDelegate {
@@ -446,7 +447,11 @@ private extension TUSAPI {
446447
weak var api: TUSAPI?
447448

448449
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
449-
api?.handleCompletionOfTask(dataTask, withError: nil, data: data)
450+
guard let api, let identifier = dataTask.taskDescription else {
451+
return
452+
}
453+
454+
api.taskData[identifier, default: Data()].append(data)
450455
}
451456

452457
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
@@ -458,14 +463,15 @@ private extension TUSAPI {
458463
}
459464
}
460465

461-
func handleCompletionOfTask(_ task: URLSessionTask, withError error: Error?, data: Data? = nil) {
466+
func handleCompletionOfTask(_ task: URLSessionTask, withError error: Error?) {
462467
queue.sync {
463468
guard let identifier = task.taskDescription else {
464469
return
465470
}
466471

467472
defer {
468473
callbacks.removeValue(forKey: identifier)
474+
taskData.removeValue(forKey: identifier)
469475
}
470476

471477
guard let completion = callbacks[identifier] else {
@@ -482,6 +488,7 @@ private extension TUSAPI {
482488
return
483489
}
484490

491+
let data = taskData[identifier]
485492
let success = (data, response)
486493
completion(.success(success))
487494
}

0 commit comments

Comments
 (0)