java - How to synchronize / limit certain async http calls in android -


i making use of android async http library in app make async http requests.

i have come situation in android app following happens. web api makes use of access token , refresh token. on every request make check if access token still valid. if not issue http post go new access token using refresh token.

now have noticed following situation.

user of app leaves phone inactive enough time access token expire. when wake phone up. in onresume() function fire off 2 separate http requests.

  1. request 1 checks access token , determines not valid. issues refreshaccesstoken request.
  2. while request 1 waiting response, request 2 checks access token , determines not valid. , issues refreshaccesstoken request.
  3. request 1 returns , updates access token , refresh token values.
  4. request 2, gets 401 response api refresh token provided has been used. application thinks there error refreshtoken , logs user out.

this incorrect , avoid it. have in mean time, done double check in refreshaccesstoken onfailed() method. see if accesstoken maybe valid again. inefficient still sedning 2 requests on air, , api has handle failed refresh attempt.

question: issue cant use locks or synchronization cannot block main ui thread in android. , android async http library handles of different threads etc.

request 2 checks access token , determines not valid.

this wrong. since request 1 may have issued refreshaccesstoken request, state of access token cannot determined consulting server.

so need combined operation getaccesstoken() checks access token, issues refreshaccesstoken when needed, and, when called in parallel, waits called getaccesstoken() operation.

update. refreshaccesstoken part of class serves gatekeeper , allows requests run if access token refreshed. if token not refreshed, gatekeeper sends single request refresh token. meantime, input requests saved in queue. when token refreshed, gatekeeper lets saved requests run.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -