Does NSLock.lock () execute even though the lock has already been saved?

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?

+4
source share
1 answer

refreshTokens, lock()/unlock(), . , @escaping responseJSON refreshTokens. , should unlock , refreshTokens.

, , , , . , , , .

+4

Source: https://habr.com/ru/post/1657884/


All Articles