I am looking at a sample of Alamofire Retriever Code:
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)
}
}
I donโt understand how you can have lock.lock()functions in the first line and then also have the same line strongSelf.lock.lock()in the closure passed in refreshTokens.
If the first lock is not released before the end of the method shouldwhen unlocking defer, then how is the second strongSelf.lock.lock()successfully executed during the first lock?
source
share