I am a developing an Android app in Android Studio.
Let's say i have a method checkSomething
and doSomething
.
In my doSomething
method I have a piece of code that looks like this:
int i = checkSomething();
if(i >= x){
runThisMethod();
}else{
runThis();
}
During the debugging I found out that my code proceeds to the if statement
goes to else
and then executes checkSomething()
method, which is too late because we are already past the if statement
I am not sure on how to fix this problem since I never had one like this.
//Edit
In my checkSomething
method I am receiving data from Firebase and checking it versus data in an ArrayList. Could that cause the delay? If so how can I fix it?
My checkSomething
method.
public ArrayList getPlayerItems(final String itemName){
final ArrayList<String> a1 = new ArrayList<>();
mDatabase.child("users").orderByChild("email").equalTo(u.getEmail()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
for(Shop.shopItem s : so){
if(userSnapshot.child("playerItems").child(s.getItemName()).exists() && s.getItemName().equals(itemName)){
a1.add(itemName);
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
return a1;
}