0

I have a method that calls 2 services that make AsyncCallBacks

    centroService.buscarCentroPorNombre(nombreCentroSeleccionado, new AsyncCallback<Centro>() { 
        @Override                                                                               
        public void onSuccess(Centro centro) {                                                  
            cArticuloCentro.setIdCentro(centro.getIdCentro());
            cArticuloCentro.setPrecio(Double.parseDouble(precioTextBox.getText())); 
        }

        @Override
        public void onFailure(Throwable caught) {
            //do something
        }
    });
    articuloService.buscarArticuloPorNombre(nombreArticuloSeleccionado, new AsyncCallback<Articulo>() { //se llama al sevivio para q busque el la base de datos la Entity por nombre
        public void onSuccess(Articulo articulo) {
            cArticuloCentro.setIdArticulo(articulo.getCod()); 
        }
        @Override
        public void onFailure(Throwable caught) {
            //do something
        }
    });

the problem comes when the next method is called becouse these serviceCalls are asynchronous method activates before the calls are made, does not getting desired data. next method is

        save(){
            articuloCentroService.saveArticuloCentro(cArticuloCentro, new AsyncCallback<String>() {
                @Override
                public void onFailure(Throwable caught) {
                    //do something
                }
                @Override
                public void onSuccess(String result) {
                    Window.alert("saved");
                }
            });
        }

please can you tell me a way to make save() method execute when the asyncCallbacks have finished thank you

1 Answers1

0

In pure java you would need to synchronize threads, but in GWT, there's only one thread running at all times, so you can do a simply sync logic using an array:

final int[] sync = new int[1];

centroService.buscarCentroPorNombre(nombreCentroSeleccionado, new AsyncCallback<Centro>() { 
@Override   
public void onSuccess(Centro centro){                                                  
        //...
        if (++sync[0] == 2){
            save();
        }
    }

    @Override
    public void onFailure(Throwable caught) {
        //do something
    }
});

articuloService.buscarArticuloPorNombre(nombreArticuloSeleccionado, new AsyncCallback<Articulo>() {
    public void onSuccess(Articulo articulo) {
        //...
        if (++sync[0] == 2){
            save();
        }
    }
    @Override
    public void onFailure(Throwable caught) {
        //do something
    }
});

Explanation: since there's only one thread running, the sync array will only be updated by one thread at a time. You can't control which method will finish first, but when they do, only one callback will be executed at a time. The sync array is just a counter you can use to sync any number of async invocations.

Gilberto Torrezan
  • 5,113
  • 4
  • 31
  • 50
  • Hi Gilberto - the comment has nothing to do with the question/answer - unfortunately it's the only way to send you a notification - I remove the comments once they've been viewed so as to not pollute the answer for too long. Pop into http://chat.stackoverflow.com/rooms/41570/so-close-vote-reviewers if you'd like to discuss the review. Thanks! – Rob Mar 03 '16 at 23:50