title: How to set a priority of retrofit requests
body: <p>I have <code>ViewPager</code> and in this ViewPager there are 4 fragments. Sorry but I can't to add a code, because that would be very big. Each of my fragments on create does asynchronous retrofit request to server. I have <code>access_token</code> and <code>refresh_token</code> too. So when my <code>access_token</code> expires I need to do a call using <code>refresh_token</code> to update my <code>access_token</code>. So when I launch my application it opens viewPager with my 4 fragments doing asynchronous requests. That means 4 requests at a time. And when my access_token is expired and I'm launching my app, all the fragments try to update the access token using refresh token. Turns out that one fragment updates the access_token (after the update refresh_token changes) other fragments try to update the token too, but I'm getting 401 because 1 fragment already updated the token and other fragments try to update the access_token using  old refresh token. </p>
  
  <p>Here is the example of one request.</p>
  
  <pre><code>Call&lt;ResponseBody&gt; getChatImageCall = apiClient.getImages(cutMessage, CHAT_IMAGE_TYPE);
  getChatImageCall.enqueue(new Callback&lt;ResponseBody&gt;() {
          @Override
          public void onResponse(Call&lt;ResponseBody&gt; call, final Response&lt;ResponseBody&gt; response) {
              if (response.isSuccessful()) {
                  try {
                      messagesAdapter.addToStart(MessagesFixtures.getImageBytesMessage(response.body().bytes(), id), true);
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              } else {
                  Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_SHORT).show();
              }
          }
  
          @Override
          public void onFailure(Call&lt;ResponseBody&gt; call, Throwable t) {
              Toast.makeText(getActivity(), "An error occurred", Toast.LENGTH_SHORT).show();
          }
      });
  </code></pre>
  
  <p>Here is the token update request part</p>
  
  <pre><code>public class RefreshToken {
  
  Context context;
  TokenUpdaterCallback tokenUpdaterCallback;
  ApiClient apiClient = ApiClient.getInstance();
  
  public RefreshToken(Context context, TokenUpdaterCallback tokenUpdaterCallback) {
      this.context = context;
      this.tokenUpdaterCallback = tokenUpdaterCallback;
  }
  
  public void updateToken() {
  
      //Perform this call if access token is expired
  
      SupportObjToken supportObjToken = new SupportObjToken();
  
      supportObjToken.setGrantType("refresh_token");
      supportObjToken.setClientId(SharedPreferencesManager.getInstance().getUserData().getClientId());
      supportObjToken.setClientSecret(SharedPreferencesManager.getInstance().getUserData().getClientSecret());
      supportObjToken.setRefreshToken(SharedPreferencesManager.getInstance().getUserData().getRefreshToken());
  
      Call&lt;RefreshTokenActivation&gt; newToken = apiClient.newToken(supportObjToken);
      newToken.enqueue(new Callback&lt;RefreshTokenActivation&gt;() {
          @Override
          public void onResponse(Call&lt;RefreshTokenActivation&gt; call, Response&lt;RefreshTokenActivation&gt; response) {
              if (response.isSuccessful()) {
                  String newAccessToken = response.body().getAccessToken();
                  String newRefreshToken = response.body().getRefreshToken();
  
                  UserData userData = SharedPreferencesManager.getInstance().getUserData();
                  SharedPreferencesManager.getInstance().removeUser();
                  userData.setAccessToken(newAccessToken);
                  userData.setRefreshToken(newRefreshToken);
                  SharedPreferencesManager.getInstance().setUser(userData);
  
                  tokenUpdaterCallback.updateToken();
              } else {
                  if (response.code() == 401) {
  
                      //Perform this call if refresh token is expired
  
                      Activity activity = (Activity) context;
                      MainActivity mainActivity = (MainActivity) activity;
                      mainActivity.logOut();
                      Toast.makeText(activity, "You logged on other hardware", Toast.LENGTH_SHORT).show();
                  }
              }
          }
  
          @Override
          public void onFailure(Call&lt;RefreshTokenActivation&gt; call, Throwable t) {
              Toast.makeText(context, "Response is not successful", Toast.LENGTH_SHORT).show();
          }
      });
  }
  }
  
  tokenUpdaterCallback is a simple interface with updateToken() method. So you don't need this.
  </code></pre>