1

I currently have the following relevant code-snippet:

public class MyEntryPoint implements EntryPoint {

    private boolean areFieldsEnabled = false;

    @Override
    public void onModuleLoad(){
        addFields();
    }

    private void addFields(){
        Button button = new Button("Show fields");
        button.addClickHandler(new ClickHandler(){

            @Override
            public void onClick(final ClickEvent event){
                String entityType = ...;
                String entityId = ...;
                checkIfFieldsAreEnabled();
                MyEntryPoint.this.fields = new FieldsValuesDiv(entityType, 
                    Long.valueOf(entityId), MyEntryPoint.this.areFieldsEnabled);
                wrapper.add(MyEntryPoint.this.fields);
            }
        });
    }

    private void checkIfFieldsAreEnabled(){
        this.fieldsService.areValuesEditable(new AsyncCallback<Boolean>(){

            @Override
            public void onFailure(final Throwable caught){
                Window.alert(caught.getLocalizedMessage());
            }

            @Override
            public void onSuccess(final Boolean result){
                MyEntryPoint.this.areFieldsEnabled = result;
            }
        });
    }
}

This results in a button "Show fields". When I click this, the Fields GWT-component is loaded and the fields are shown. Currently all the fields are disabled by default, even though the checkIfFieldsAreEnabled() is done right before it. When I click on the "Hide fields" button (not shown in the code above), and then the "Show fields" button again, it does work.

The reason? It doesn't wait for the result of the AsyncCallback<Boolean>, so sometimes it does work and sometimes it doesn't, depending on how fast the Async call is done. I'm coming from a C# background where async/await can be used. When I googled for a Java equivalent I came across this SO answer by Jon Skeet, which basically states there is not such a thing in Java. This was from almost three years ago however, so I wonder if anything has changed since then.

If not, does anyone know how I can await the AsyncCallback<Boolean> which sets the areFieldsEnabled boolean?

Community
  • 1
  • 1
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • Did the answer solve your problem? If not, can you please clarify? – Baz Mar 09 '16 at 10:16
  • @Baz Unfortunately no. When I read your answer it was very obvious, so I implemented it like that, but the fields are still initially disabled. I'm not entirely sure what the problem is though, and currently there were some shifts in priorities. The issue still remains though, and we do have to solve it in the near future, so I'll keep you posted when I know a bit more about what is going on. – Kevin Cruijssen Mar 09 '16 at 12:03
  • @Baz I'm not sure what changed these last few days, but now it does work after I had been working on some other features/bugs around in the same GWT project as this one. Since your solution is indeed the correct way to handle AsynCallback in Java, I've accepted it as the answer. – Kevin Cruijssen Mar 11 '16 at 13:13
  • Glad it worked in the end – Baz Mar 11 '16 at 13:47

1 Answers1

3

The callback is run asynchronously, meaning your other code (the lines after the call) are executed while the callback is still getting your data from the server.

If you only want to execute something once the callback returns, call it from the onSuccess method of the callback:

this.fieldsService.areValuesEditable(new AsyncCallback<Boolean>(){

    @Override
    public void onFailure(final Throwable caught){
        Window.alert(caught.getLocalizedMessage());
    }

    @Override
    public void onSuccess(final Boolean result){
        MyEntryPoint.this.areFieldsEnabled = result;
        // Do whatever needs to be done here
        MyEntryPoint.this.fields = new FieldsValuesDiv(entityType, Long.valueOf(entityId), MyEntryPoint.this.areFieldsEnabled);
        wrapper.add(MyEntryPoint.this.fields);
    }
});

This will make sure that areFieldsEnabled is set before you try to access it.

Baz
  • 36,440
  • 11
  • 68
  • 94