5

I've been having trouble the last week or so retrieving data from my Firebase database. I've searched all over StackOverflow and google for the answer to this and either I'm not understanding the solutions being presented or they simply aren't working for me. Everything is being called successfully until I get to the valueEventListener. I've logged everything and it never triggers. The error handler doesn't return any errors.

public class DBHandler {
    private FirebaseAuth mAuth = FirebaseAuth.getInstance();

    private String key;

    public ArrayList<String>tips,tables,times;

    private DatabaseReference mDatabase;

    private DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
    private DatabaseReference mPostRef = mRootRef.child("user-posts");
    private DatabaseReference mIdRef = mPostRef.child(mAuth.getCurrentUser().getUid());
    private DatabaseReference mKeyRef;//move down
    private DatabaseReference mTimeRef;
    private DatabaseReference mTipRef;
    private DatabaseReference mTableRef;

    public DBHandler(){
        //Constructor
    }

    public DBHandler(String key) {
        tips(key);
        Log.d("Test for tips()", "Triggered");
    }

    public void tips(String key){
        mKeyRef = mIdRef.child(key);
        mTipRef = mKeyRef.child("tips");

        Log.d("Test for reference", "mTipRef: " + mTipRef.getRef());

        mTipRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.d("Test for listener", "Triggered");

                ArrayList<String> placeholder= new ArrayList<String>();
                placeholder = dataSnapshot.getValue(ArrayList.class);
                Log.d("Test for placeholder", "placeholder: " + placeholder);

                tipsHolder(placeholder);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) { }
        });

    }

    private void tipsHolder(ArrayList<String> placeholder) {
        this.tips = placeholder;
    }

    public void times(String key){ }
    public void tables(String key){ }
    public void tablesHolder(ArrayList<String> placeholder){ }
    public void timesHolder(ArrayList<String> placeholder){ }
}

Thanks in advance to any and everyone who attempts to help me resolve this headache.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Vell
  • 391
  • 2
  • 9
  • I copied your code and ran it. The listener fires for me when there is a network connection. I can make it not fire by running with no connection. Are you certain you have a network connection? You can monitor connection state as described in [this documentation](https://firebase.google.com/docs/database/android/offline-capabilities#section-connection-state). – Bob Snyder Jul 20 '16 at 05:32
  • @qbix Thanks for your reply. I tried using the connection test in the dev docs you linked, but got the same behavior as I'm getting with my original Listener, its simply not calling the listener. The fact that it works for you however sheds some insight into a possibility. As of now I'm creating the Dbhandler as an object and then trying to pull information from that object. Maybe I should try another approach? – Vell Jul 20 '16 at 17:31
  • 1
    I've not seen listeners used in a class the way you have done it. Maybe unconventional, but should work. In my test I simply created an instance of `DBHandler`. Don't put the connection listener in `DBHandler`. Put it in the `onCreate()` of your main activity. You can also try enabling debug logging: `FirebaseDatabase.getInstance().setLogLevel(Logger.Level.DEBUG)`. – Bob Snyder Jul 20 '16 at 17:43
  • I moved the connection listener to the main activity and put it in the onCreate as you suggested and it connected successfully. So it seems like for some reason when its in the DBHandler its not getting enough time to respond. – Vell Jul 20 '16 at 17:58
  • I just noticed that you have no debug log statement in the `onCancelled()` method of the listener. Put something in there. I'll bet it is firing to report some error, possibly access failure. Do you have the security rules set for your database? – Bob Snyder Jul 20 '16 at 18:10
  • Sorry for the delayed response, but I have implemented Logs in the onCancelled as well and it won't return any errors. Its simply not calling anything that has to do with the Listener. I think it has something to-do with the value not being updated to trigger the listener. I'm going to try initializing the Database Reference in the main activity and pass it via bundles. – Vell Jul 25 '16 at 20:44
  • Got the same issue, in my case happened that I had relocated the `removeEventListener` from my previous workflow and did not relocate, I know is silly but I post my comment maybe somebody may save time. – Guanaco Devs Jan 08 '18 at 12:17

0 Answers0