-
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.
Use URLSession delegate methods to allow background URLSessions in Tr…
…ansloaditKit (#39) * Started migrating to a delegate based URLSession task system * ensure callbacks are called and removed when no longer needed * CI fixes * Swift tools bump * Various test fixes
- Loading branch information
Showing
9 changed files
with
117 additions
and
42 deletions.
There are no files selected for viewing
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
27 changes: 27 additions & 0 deletions
27
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Foundation | ||
|
||
extension TransloaditAPI: URLSessionDataDelegate { | ||
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { | ||
guard let completionHandler = callbacks[task] else { | ||
return | ||
} | ||
|
||
defer { callbacks[task] = nil } | ||
|
||
if let error { | ||
completionHandler.callback(.failure(error)) | ||
return | ||
} | ||
|
||
guard let response = task.response else { | ||
completionHandler.callback(.failure(TransloaditAPIError.incompleteServerResponse)) | ||
return | ||
} | ||
|
||
completionHandler.callback(.success((completionHandler.data, response))) | ||
} | ||
|
||
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: 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
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
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