Why does my variable not get updated after the onComplete method has finished?
I have a public int variable 'numPoints'. When I call a method 'CalculateBatPoints' I update the variable numPoints inside of a onComplete method. However, when the onComplete method is complete, the numPoints value doesn't appear to have been updated.
private void CalculateBatPoints(ArrayList<String> listBat){
numPoints = 0;
for (int i=0;i<listBat.size();i++){
db.collection("players").document(listBat.get(i)).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
DocumentSnapshot ds = task.getResult();
numPoints = Integer.parseInt(ds.getString("gwPoints")) + numPoints;
System.out.println(numPoints);
}
});
}
System.out.println("numPoints is " + numPoints);
}
The output I got was: 32 158 185 271 numPoints is 0
I expected the output: 32 158 185 271 numPoints is 271