Wracking my brain on this. I have an app using OAuth2 where I request a token which has an expiration. I am trying to figure out the best way to check for the expiration and if it is expired, request a new token before I send my new API request. I have an enum of the Router class from AlamoFire that checks for an access token like so:
UserManager.getUser()?.getUserAccessToken()
That method looks like:
func getUserAccessTokenForRequest() -> String? {
if isTokenValid() {
return self.accessToken
}
else {
var localAccessToken : String?
let request = DataManager.currentRequest as Request?
DataManager.currentRequest?.cancel()
DataManager.refreshToken{ (success, value) in
if success {
if let returnedToken : String = value as? String {
request?.resume()
}
} else {
if let localError: AnyObject = value {
}
}
}
return nil;
}
}
However when I go to resume the initial request, it is nil because it has already completed and received a response that the token was invalid. I am running into the issue of Async tasks completing before I would like them too (because they are async). I am wondering what the best architecture for this would look like. I could check if the token is valid in my data manager before I even make a request. However I don't want to have to do that for every call.
Would overriding the create request method be the best way to go?