简体   繁体   中英

Firebase ValueEventListener not firing at all

I am simply trying to fire this Firebase ValueEventListener. I have set break points inside of the listeners to check if they are firing, and they are not. I am not sure why it isn't working, as everything looks fine to me. Is there any reason relating to Firebase that this app would not be working?

package sampleproj.wisen.android.sampleproj;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DatabaseReference d = FirebaseDatabase.getInstance().getReference();
        d.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                int r = 0;
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    }
}

here is the structure of the database: 这是数据库的结构: 在此处输入图片说明

I have already tried accessing the database at the deepest child level. The point is to retrieve a list of messages between two users.

    final String senderUsername = "WiseNN";
    final String recipeintUsername = "tellMeWhen2Go";
    final String messagesKey = "messages";
    DatabaseReference DBRef = FirebaseDatabase.getInstance().getReference();
            DatabaseReference DBMsgsRef = DBRef.child("privateChat").child(senderUsername).child(recipeintUsername).child(messagesKey).getRef();

尝试在getReference()方法"privateChat/WiseNN/tellMeWhen2Go/messages/"作为数据库引用。

DatabaseReference d = FirebaseDatabase.getInstance().getReference("privateChat/WiseNN/tellMeWhen2Go/messages/");
String data, message;
DatabaseReference d = FirebaseDatabase.getInstance().getReference("privateChat").child("WiseNN").child("tellMeWhen2Go").child("messages");
d.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            data = ds.child("data").getValue().toString();
            message = ds.child("message").getValue().toString();
            // similarly for sender & time

        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

I am writing my own answer, as I have just figured it out.

I think this was due to some sort of Firebase glitch. When I logged into my google console, I saw that I had no database (there was nothing in it), when I know in-fact one existed. However, this would make sense as to why the callback functions were not firing.

Oddly enough, I cloned a new example firebase android app from GitHub, added it to my Firebase app in the google console, then wrote a new object to the realtime-database (from the new example android app). After doing this, my entire firebase database structure literally reappeared.

After that, I went back to the main app I was originally working on, and called firebase using the exact same code that was previously there...and it worked. I know this may not sound reasonable, but this is literally what just happened.

You missed .child("messages");

DatabaseReference DBRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference DBMsgsRef = DBRef.child("privateChat").child(senderUsername).child(recipeintUsername).child("messages").child(messagesKey).getRef();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM