In a loop, I need to call multiple times a method with callback . How can I know when all is finished ?
@Override
public void onObjectsMustBeParsed(String parsableObjects) {
String[] parsedObjects = parsableObjects.split(",");
for (String parsedObject : parsedObjects){
loadObject(parsedObject, new LoadObjectCallback() {
@Override
public void onObjectLoaded(Object object) {
//Object Loaded
saveObject(object, new SaveObjectCallback() {
@Override
public void onObjectSaved() {
// Object saved
}
@Override
public void onError() {
// Object not saved
}
});
}
@Override
public void onError(Throwable throwable) {
// Object not Loaded
}
});
}
}
// => do something when all processing of parsed-objects are finished
// do something if all is ok
// do other thing if partially ok
Note : To manipulate my data, I use a repository with local and remote data sources. This piece of code is a part of repository.
Add a volatile integer which indicates the amount of running tasks. Increment when you start a task. Decrement in onObjectLoaded or in onObjectSaved. Then after every decrement check if the task counter is nul.