-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ensure callbacks are called and removed when no longer needed
- Loading branch information
Showing
5 changed files
with
53 additions
and
18 deletions.
There are no files selected for viewing
16 changes: 9 additions & 7 deletions
16
Sources/TransloaditKit/TransloaditAPI+URLSessionDelegate.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
import Foundation | ||
|
||
extension TransloaditAPI: URLSessionDelegate { | ||
extension TransloaditAPI: URLSessionDataDelegate { | ||
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { | ||
guard let callback = callbacks[task]?.1 else { | ||
guard let completionHandler = callbacks[task] else { | ||
return | ||
} | ||
|
||
defer { callbacks[task] = nil } | ||
|
||
if let error { | ||
callback(.failure(error)) | ||
completionHandler.callback(.failure(error)) | ||
return | ||
} | ||
|
||
guard let data = callbacks[task]?.0, let response = task.response else { | ||
//callback(.failure(TransloaditAPIError.unknown)) | ||
guard let response = task.response else { | ||
completionHandler.callback(.failure(TransloaditAPIError.incompleteServerResponse)) | ||
return | ||
} | ||
|
||
callback(.success((data, response))) | ||
completionHandler.callback(.success((completionHandler.data, response))) | ||
} | ||
|
||
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { | ||
callbacks[dataTask]?.0?.append(data) | ||
callbacks[dataTask]?.data.append(data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Foundation | ||
|
||
class URLSessionCompletionHandler { | ||
var data: Data | ||
let callback: (Result<(Data, URLResponse), Error>) -> Void | ||
|
||
init(callback: @escaping (Result<(Data, URLResponse), Error>) -> Void) { | ||
self.callback = callback | ||
self.data = Data() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters