我正在查看一些 Alamofire示例Retrier 代码:
func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) {
lock.lock() ; defer { lock.unlock() }
if let response = request.task.response as? HTTPURLResponse, response.statusCode == 401 {
requestsToRetry.append(completion)
if !isRefreshing {
refreshTokens { [weak self] succeeded, accessToken, refreshToken in
guard let strongSelf = self else { return }
strongSelf.lock.lock() ; defer { strongSelf.lock.unlock() }
...
}
}
} else {
completion(false, 0.0)
}
}
我不知道如何lock.lock()
在函数的第一行使用,然后strongSelf.lock.lock()
在闭包中将同一行传递给refreshTokens
.
如果在执行解锁时直到should
方法结束才释放第一个锁,那么第二个锁如何在第一个锁被持有时成功执行?defer
strongSelf.lock.lock()