-1

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;
    }
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Duzy
  • 85
  • 12
  • It would be better if you showed what all the code is doing, rather than leaving out the implementation of those methods you're showing. – Doug Stevenson Apr 17 '19 at 20:45
  • In the described case, checkSomething _is_ executed before the if statement. It is impossible to get a "placeholder" return value that will stand in for a "real" execution of a function. There might be some other code calling checkSomething later on. – fdreger Apr 17 '19 at 20:53
  • @DougStevenson I have added the code to my question. I am aware that it could be better optimized but I just want to get it working first. – Duzy Apr 17 '19 at 21:01
  • 1
    Your `getPlayerItems` is going to return an empty array every time. addListenerForSingleValueEvent listener is asynchronous and returns immediately before the database results are available. You should only use those results from inside the callback, when the results are available. – Doug Stevenson Apr 17 '19 at 21:18
  • @DougStevenson I was suspecting that.. Thank you for your answer. – Duzy Apr 17 '19 at 21:21

1 Answers1

1

As per my understanding, you are checking for the return from Firebase, which is asynchronous method, which means, that you should wait until the result from Firebase method is ready then you proceed to other checks.

If you deal with Firebase as a synchronous function, then you will not get correct result, because you are checking for a value that still doesn't exist. It will exist after few time. This is why you should correctly implement the Firebase functions.

If you are using Firebase Realtime database, you will find good examples on the reference page Read and Write Data on Android

Hope this could help