|
|
@@ -66,6 +66,8 @@ open class Session {
|
|
|
var requestTaskMap = RequestTaskMap()
|
|
|
/// Set of currently active `Request`s.
|
|
|
var activeRequests: Set<Request> = []
|
|
|
+ /// Completion events awaiting `URLSessionTaskMetrics`.
|
|
|
+ var waitingCompletions: [URLSessionTask: () -> Void] = [:]
|
|
|
|
|
|
/// Creates a `Session` from a `URLSession` and other parameters.
|
|
|
///
|
|
|
@@ -1017,23 +1019,44 @@ extension Session: RequestDelegate {
|
|
|
|
|
|
extension Session: SessionStateProvider {
|
|
|
func request(for task: URLSessionTask) -> Request? {
|
|
|
+ dispatchPrecondition(condition: .onQueue(rootQueue))
|
|
|
+
|
|
|
return requestTaskMap[task]
|
|
|
}
|
|
|
|
|
|
func didGatherMetricsForTask(_ task: URLSessionTask) {
|
|
|
- requestTaskMap.disassociateIfNecessaryAfterGatheringMetricsForTask(task)
|
|
|
+ dispatchPrecondition(condition: .onQueue(rootQueue))
|
|
|
+
|
|
|
+ let didDisassociate = requestTaskMap.disassociateIfNecessaryAfterGatheringMetricsForTask(task)
|
|
|
+
|
|
|
+ if didDisassociate {
|
|
|
+ waitingCompletions[task]?()
|
|
|
+ waitingCompletions[task] = nil
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- func didCompleteTask(_ task: URLSessionTask) {
|
|
|
- requestTaskMap.disassociateIfNecessaryAfterCompletingTask(task)
|
|
|
+ func didCompleteTask(_ task: URLSessionTask, completion: @escaping () -> Void) {
|
|
|
+ dispatchPrecondition(condition: .onQueue(rootQueue))
|
|
|
+
|
|
|
+ let didDisassociate = requestTaskMap.disassociateIfNecessaryAfterCompletingTask(task)
|
|
|
+
|
|
|
+ if didDisassociate {
|
|
|
+ completion()
|
|
|
+ } else {
|
|
|
+ waitingCompletions[task] = completion
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func credential(for task: URLSessionTask, in protectionSpace: URLProtectionSpace) -> URLCredential? {
|
|
|
+ dispatchPrecondition(condition: .onQueue(rootQueue))
|
|
|
+
|
|
|
return requestTaskMap[task]?.credential ??
|
|
|
session.configuration.urlCredentialStorage?.defaultCredential(for: protectionSpace)
|
|
|
}
|
|
|
|
|
|
func cancelRequestsForSessionInvalidation(with error: Error?) {
|
|
|
+ dispatchPrecondition(condition: .onQueue(rootQueue))
|
|
|
+
|
|
|
requestTaskMap.requests.forEach { $0.finish(error: AFError.sessionInvalidated(error: error)) }
|
|
|
}
|
|
|
}
|