How to get variable from the asynchronous call back?

Hello,

I’m developing an android app and I want to make a logic layer for the queries of the database.
I have the following code:

Backendless.Persistence.of(Unit.class).find(dataQueryUnit, new AsyncCallback<BackendlessCollection<Unit>>() {
@Override
public void handleResponse(BackendlessCollection<Unit> unitBackendlessCollection) {
BackendlessCollection<Unit> resultUnits = unitBackendlessCollection;
List<Unit> unitList = resultUnits.getData();
int numberOfUnits = unitList.size();
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
// Toast.makeText(SelectUnit.this, “error”, Toast.LENGTH_SHORT).show();
}
});

I want to make a function that returns the numberOfUnit variable. The problem is that I can’t access that variable.

I tried using a “final” variable but it didn’t work.

Hi Miriam,
You need to move the unit of work you need perform after request to handleResponse method.
In computer programming such paradigm is called Callback. Read more at https://en.wikipedia.org/wiki/Callback_(computer_programming).
Best wishes, Artur