Alamofire RequestRetrier,调用主操作队列上的完成块可以吗?
Alamofire RequestRetrier, is it ok to call the completion block on the main operation queue?
我一直想知道从主队列上的请求重试调用完成块是否可以,因为函数调用是在 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)
}
}
}
docu 没有明确说明,但如果我没记错的话,通常期望在与函数调用相同的队列中调用完成块
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)
所以我的问题是,应该在哪个队列上调用完成?
是的,它是安全的,因为调用重试器的内部实现会立即回调到 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))
}
}
我一直想知道从主队列上的请求重试调用完成块是否可以,因为函数调用是在 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)
}
}
}
docu 没有明确说明,但如果我没记错的话,通常期望在与函数调用相同的队列中调用完成块
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)
所以我的问题是,应该在哪个队列上调用完成?
是的,它是安全的,因为调用重试器的内部实现会立即回调到 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))
}
}