I have created a network manager class which inherits from AFHTTPSessionManager and I have defined methods for GET, POST, etc. in the class. Here is the POST implementation -
- (NSURLSessionTask*)performPOSTRequestToURL:(NSString*)postURL andParameters:(NSDictionary*)parameters withCompletionBlock:(WebServiceCompletionResponse)completionBlock {
[self POST:postURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
/* Token is still valid, got success response / data */
if (completionBlock) {
completionBlock(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if ([error code] == SESSION_EXPIRED_CODE) { /* Token has expired because error code is SESSION_EXPIRED_CODE */
/* Make call to refresh token */
[[TokenLibrary sharedInstance] refreshToken:^(NSError *error, NSString *token) {
if (error) {
completionBlock(error);
}
else{
/* Got refreshed token, I want to call performPOSTRequestToURL with the same url now. How do I do it? Is the call below okay? */
[self performPOSTRequestToURL:postURL andParameters:parameters withCompletionBlock:completionBlock];
}
}];
}
}];
}
I have put in my question in the last comment above.
Basically, I make my network calls to post or get data and if there is a SERVER_EXPIRED_CODE as error response I get from webservice, I need to refresh the token first and then make the same API call again.
Can I directly make the method call in place of the last comment? Or there is another or better ways to do it?