简体   繁体   English

Firebase Realtime返回null但存在数据

[英]Firebase Realtime returns null but data exists

I'm facing an issue regarding the Firebase Realtime database and in particular the value event listener which fires more than once .Seems that when the Internet state changes from on to off several times and the device finally has stable connection the onDataChange(DataSnapshot dataSnapshot) callback method of the listener is invoked with dataSnapshot of null content .Seems that the Realtime Database refers to the app's local cache and in that case I do not have any data stored in it. 我遇到了与Firebase Realtime数据库有关的问题,尤其是触发多次事件的value事件侦听器。似乎当Internet状态从开变为关多次并且设备最终具有稳定的连接时, onDataChange(DataSnapshot dataSnapshot)侦听器的回调方法使用null内容的dataSnapshot调用。似乎实时数据库引用了应用程序的本地缓存,在这种情况下,我没有任何数据存储在其中。 I am attaching the listener inside the Activity onStart() or when the device has established some connection ; 我将侦听器附加到Activity onStart()内,或者在设备建立了某些连接后; I am detaching the listener inside the Activity onStop() method or when the device looses internet connection .Only one instance of a given listener exists at a time and every attach has corresponding detach action executed when needed. 我在Activity onStop()方法内部或在设备断开Internet连接时分离侦听器。一次仅存在一个给定侦听器的实例,并且每个附加操作都在需要时执行相应的分离操作。 I have tried to wait a while between the change of the connection states before attaching the listener and to reattach the listener if the datasnapshot returns null .None of those worked.Please advice for a solution. 我试图在连接状态更改之间等待一会儿,然后再连接侦听器,如果datasnapshot返回null则重新连接侦听器,但均无效,请提供解决方案。

Some example code inside an Activity : Activity中的一些示例代码:

private ValueEventListener listener;
private Query query;
private boolean hasAttachedListener;

private Query getDatabaseReference() {

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    return reference.child(“some child ref”)
            .child(“other child ref 2 ”);
}

private ValueEventListener getDatabaseListener() {

    return new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            Log.d(“firebase”, dataSnapshot.toString());
    //issue here datasnapshot is null sometimes
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.d(“firebase”, databaseError.getDetails());
        }
    };
}


/**
 * Attaches  listener
 */
public void addListener() {

    if (!hasAttachedListener) {
        query = getDatabaseReference();
        listener = getDatabaseListener();
        query.addValueEventListener(listener);
        hasAttachedListener = true;
    }
}

/**
 * Detaches the attached listener
 */
public void removeListener() {

    if (hasAttachedListener) {
        query.removeEventListener(listener);
        query = null;
        listener = null;
        hasAttachedListener = false;
    }
}

@Override
protected void onStart() {
    super.onStart();
    addListener();
}

@Override
protected void onStop() {
    super.onStop();
    removeListener();
}

@Override
protected void onNetworkDisconnected() {
    super.onNetworkDisconnected();
    // invoked when internet connection is lost 
    removeListener();

}

@Override
protected void onNetworkReconnected() {
    super.onNetworkReconnected();
    // invoked when internet connection is restored 
    addListener();
}

With firebase offline capabilities you are not needed to use those two method to listen if there is no connection to the database 使用Firebase离线功能,如果没有与数据库的连接,则无需使用这两种方法进行侦听

so your onNetworkDisconnected and onNetworkReconnected are not necesary 因此您的onNetworkDisconnectedonNetworkReconnected不需要

check the firebase docs here : https://firebase.google.com/docs/database/android/offline-capabilities 在此处查看Firebase文档: https : //firebase.google.com/docs/database/android/offline-capabilities

Keeping Data Fresh 保持数据新鲜

The Firebase Realtime Database synchronizes and stores a local copy of the data for active listeners. Firebase实时数据库为活动侦听器同步并存储数据的本地副本。 In addition, you can keep specific locations in sync. 此外,您可以使特定位置保持同步。

DatabaseReference scoresRef = FirebaseDatabase.getInstance().getReference("scores");
scoresRef.keepSynced(true);

The Firebase Realtime Database client automatically downloads the data at these locations and keeps it in sync even if the reference has no active listeners. 即使引用没有活动的侦听器,Firebase Realtime Database客户端也会自动在这些位置下载数据并保持同步。 You can turn synchronization back off with the following line of code. 您可以使用以下代码行关闭同步。

新数据应具有唯一名称,以防止替换现有数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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