1

Please see edits, even if you don't know firebase this is probably a normal java issue Please note, this is for the Java server version of the firebase API. I have a class implementing ValueEventListener, which can be seen as follows:

public abstract class BalanceResultListener implements ValueEventListener {
    private final SteamUser user;

    public BalanceResultListener(SteamUser user) {
        this.user = user;
    }

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Log.d(getClass(),"BalanceResultListener for "+user.getUid()+" got data!");
        onBalanceChange(user, (int) dataSnapshot.child("keys").getValue());
    }

    public abstract void onBalanceChange(SteamUser user, int balance);
}

This is then being extended like follows:

private class BalanceCheckListener extends BalanceResultListener {
    // Snip, not relevant

    BalanceCheckListener(SteamUser user, int betAmount) {
        super(user);
        Log.d(getClass(),"BalanceCheckListener for "+user.getUid()+" created!");
        // Snip, not relevant
    }

    @Override
    public void onBalanceChange(SteamUser user, int balance) {
        Log.d(getClass(),user.getUid()+" has a balance of "+balance);
        // Snip, not relevant
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.d(getClass(),user.getUid()+"'s bet failed due to a db error (1)");
        // Snip, not relevant
    }
}

And finally, this listener is being added as follows:

<DatabaseReference>.addListenerForSingleValueEvent(new BalanceCheckListener(user, betAmount));

Now, The debug message in the BalanceCheckListener constructor is being called, so I know it's getting to that point; however, the program waits about 3 seconds then terminates as if the listener ran and returned. The debug messages for onDataChange and onCancelled are not being called (I've double checked it isn't Log by using plain System.outs) Neither the BalanceCheckListener (or really just the ValueEventListener)'s onDataChange or onCancelled methods are being called. If the database isn't encountering an error or giving me the results, what could be going on to cause a return?

EDIT: I added a 10 second sleep after the listener add call, and now finally the onCancel is running. I'm not sure why this sleep needs to happen for the error to be output (Maybe the console stream is closing before the error can be output?)

EDIT EDIT: After getting everything working correctly with the 10 second sleep and then removing it, the same return occurs even though there's no error. Could it be that the garbage collector is wiping the class because there's no reference to it? (Although there should be, because it needs to have a reference to call it...)

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
cameronlund4
  • 537
  • 1
  • 5
  • 27
  • The Firebase Database SDK interacts with its servers on a separate thread, so that your main program can continue unblocked. If your main thread exits, the Firebase thread will never retrieve its data. See http://stackoverflow.com/questions/37098006/firebase-with-java-non-android-retrive-information – Frank van Puffelen Jun 25 '16 at 22:54

0 Answers0