Syncronizing static variables in inner class

Delsh :

basically i am trying to modify a static variable from an inner class and then i want to read that static variable but it's not being set properly (from what i've read, the problem might be that the program is running in multiple threads and the thread that is reading the static value might not get an update and only read a cache?) I tried setting my static variable to volatile but had no effect.

MainActivity ->

ApiManager.evaluateResponse(responseLoginCall, MainActivity.this);

    if(ApiManager.isExecutionSuccessful) {
        Toast.makeText(this, "We are in", Toast.LENGTH_SHORT).show();
    }

ApiManager ->

public class ApiManager  {

public static volatile boolean isExecutionSuccessful = false;

public static void evaluateResponse(Call<AuthResponse> responseCall, Activity activity) {
    responseCall.enqueue(new Callback<AuthResponse>() {
        @Override
        public void onResponse(@NotNull Call<AuthResponse> call, @NotNull Response<AuthResponse> response) {
            if(response.isSuccessful()) {
                ApiManager.isExecutionSuccessful = true;
            } ...
    });

In this example isExecutionSucessful is false even if it goes through ApiManager.isExecutionSuccessful = true;

donquih0te :

ResponseCall.enqueue() asynchronously sends the request and notifies the callback of its response, so ApiManager.evaluateResponse(responseLoginCall, MainActivity.this) was called before the request completed responseCall.enqueue().

I suggest making a synchronous call with the responseCall.execute() function.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=317122&siteId=1