1

I'm creating an Android app for the first time, I've got a simple Realtime Firebase Database with a couple of records in it. I have the following code;

  public void onStart() {
    super.onStart();

    // Read from the database
    databaseMatches.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot matchSnapshot : dataSnapshot.getChildren()) {
                matches match = matchSnapshot.getValue(matches.class);

                matchesList.add(match);
            }


            matchList adapter = new matchList (getActivity(), matchesList);
            listViewMatch.setAdapter(adapter);

        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w(TAG, "Failed to read value.", error.toException());
        }
    });

If I put a breakpoint on the databaseMatches.addValueEventListener(new ValueEventListener() { it shows me that the database connection has been set and is returning the correct object (In my view).

The challenge I have is the part after, the break points for public void onDataChange nor onCancelled ever get hit. I'm lost here and not sure what might be the next step as it appears to be connecting, but I am not able to retrieve records.

I'm doing this in a fragment instead of a activity. Any help is appreciated.

Subash J
  • 2,028
  • 3
  • 13
  • 27
Carl Bruiners
  • 516
  • 1
  • 7
  • 21
  • show your database structure – Rushabh Shah Feb 24 '18 at 07:35
  • Unless you need to keep listening for updates on the database I suggest using `addListenerForSingleValueEvent` instead of `addValueEventListener`. This way you won't have to worry about removing the listener later on. – daniribalbert Mar 01 '18 at 02:09

2 Answers2

0

Detecting Connection State it is useful for your app to know when it is online or offline. Firebase Realtime Database provides a special location at /.info/connected which is updated every time the Firebase Realtime Database client's connection state changes. Here is an example: If you are not sure.
https://firebase.google.com/docs/database/android/offline-capabilities#section-connection-state

DatabaseReference connectedRef = 
FirebaseDatabase.getInstance().getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
   boolean connected = snapshot.getValue(Boolean.class);
   if (connected) {
      System.out.println("connected");
   } else {
     System.out.println("not connected");
   }
}

@Override
public void onCancelled(DatabaseError error) {
  System.err.println("Listener was cancelled");
}

Firebase also loads and synchronizes data asynchronously see Setting Singleton property value in Firebase Listener

nyulan
  • 311
  • 3
  • 12
0

Thanks.

There must have been some strange caching issue as the following morning when I ran the exact same code, no problem. And I've not had a problem since.

Carl Bruiners
  • 516
  • 1
  • 7
  • 21