I have been using PromiseKit in the project for asynchronous request processing and my goal is to present the login screen to the user if the request has failed. It is not a problem to react on the first failed request, delete the access token and present the login screen. The problem is that more than one request can fail at the same time and before the user enters new credentials more and more requests start failing.
My intended behaviour is that once the request fails, other requests are paused/waiting until the login returns valid promise and than they are executed.
It would be really helpful if any of you could offer me a guidance. I am currently thinking about the solution like this:
func login() -> Promise<Void> {
return Promise { fulfill, reject in
// Tries to present the login screen.
// Fulfill
// - accessToken already stored in Preferences
// - OR
// - successful API call
// Reject
// - if the login screen is being already presented
// - OR
// - failure on API call
}
}
func get(endpoint: Endpoint, params: Params,...) -> Promise<AnyObject> {
// Get API results and error
switch error {
case .Unauthorized:
// Delete the accessToken from Preferences
return login().then {
// Retry the request
}
default:
throw error
}
}
Aucun commentaire:
Enregistrer un commentaire