From 569f8143e126357b2a1b07e2d6348a8bd5f4bc44 Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Fri, 8 Mar 2024 13:03:28 -0800 Subject: [PATCH] Session and task descriptions (apollographql/apollo-ios-dev#286) --- Sources/Apollo/URLSessionClient.swift | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Sources/Apollo/URLSessionClient.swift b/Sources/Apollo/URLSessionClient.swift index ba9e6ce3a2..692a65efcd 100644 --- a/Sources/Apollo/URLSessionClient.swift +++ b/Sources/Apollo/URLSessionClient.swift @@ -61,12 +61,17 @@ open class URLSessionClient: NSObject, URLSessionDelegate, URLSessionTaskDelegat /// - Parameters: /// - sessionConfiguration: The `URLSessionConfiguration` to use to set up the URL session. /// - callbackQueue: [optional] The `OperationQueue` to tell the URL session to call back to this class on, which will in turn call back to your class. Defaults to `.main`. + /// - sessionDescription: [optional] A human-readable string that you can use for debugging purposes. public init(sessionConfiguration: URLSessionConfiguration = .default, - callbackQueue: OperationQueue? = .main) { + callbackQueue: OperationQueue? = .main, + sessionDescription: String? = nil) { super.init() - self.session = URLSession(configuration: sessionConfiguration, - delegate: self, - delegateQueue: callbackQueue) + + let session = URLSession(configuration: sessionConfiguration, + delegate: self, + delegateQueue: callbackQueue) + session.sessionDescription = sessionDescription + self.session = session } /// Cleans up and invalidates everything related to this session client. @@ -112,12 +117,14 @@ open class URLSessionClient: NSObject, URLSessionDelegate, URLSessionTaskDelegat /// /// - Parameters: /// - request: The request to perform. + /// - taskDescription: [optional] A description to add to the `URLSessionTask` for debugging purposes. /// - rawTaskCompletionHandler: [optional] A completion handler to call once the raw task is done, so if an Error requires access to the headers, the user can still access these. /// - completion: A completion handler to call when the task has either completed successfully or failed. /// /// - Returns: The created URLSession task, already resumed, because nobody ever remembers to call `resume()`. @discardableResult open func sendRequest(_ request: URLRequest, + taskDescription: String? = nil, rawTaskCompletionHandler: RawCompletion? = nil, completion: @escaping Completion) -> URLSessionTask { guard self.hasNotBeenInvalidated else { @@ -126,6 +133,8 @@ open class URLSessionClient: NSObject, URLSessionDelegate, URLSessionTaskDelegat } let task = self.session.dataTask(with: request) + task.taskDescription = taskDescription + let taskData = TaskData(rawCompletion: rawTaskCompletionHandler, completionBlock: completion)