我一直想知道從主佇列上的請求重試中呼叫完成塊是否可以,因為函式呼叫是在 session.rootQueue 上進行的
func retry(_ request: Request, for session: Session, dueTo error: Error, completion: @escaping (RetryResult) -> Void) {
OperationQueue.main.addOperation { [weak self] in
guard let self = self else {
completion(.doNotRetryWithError(e))
return
}
self.handleError(e, completion: completion)
}
}
}
檔案沒有明確說明這一點,但如果我沒記錯的話,通常期望在進行函式呼叫時在同一佇列上呼叫完成塊
public protocol RequestRetrier {
/// Determines whether the `Request` should be retried by calling the `completion` closure.
///
/// This operation is fully asynchronous. Any amount of time can be taken to determine whether the request needs
/// to be retried. The one requirement is that the completion closure is called to ensure the request is properly
/// cleaned up after.
///
/// - Parameters:
/// - request: `Request` that failed due to the provided `Error`.
/// - session: `Session` that produced the `Request`.
/// - error: `Error` encountered while executing the `Request`.
/// - completion: Completion closure to be executed when a retry decision has been determined.
func retry(_ request: Request, for session: Session, dueTo error: Error, completion: @escaping (RetryResult) -> Void)
所以我的問題是,應該在哪個佇列上呼叫完成?
uj5u.com熱心網友回復:
是的,它是安全的,因為呼叫您的 retrier 的內部實作會立即回呼 Alamofire 的rootQueue.
從 Alamofire 5.4.4 開始:
retrier.retry(request, for: self, dueTo: error) { retryResult in
self.rootQueue.async {
guard let retryResultError = retryResult.error else { completion(retryResult); return }
let retryError = AFError.requestRetryFailed(retryError: retryResultError, originalError:
error)
completion(.doNotRetryWithError(retryError))
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/348897.html
