Firebase Android addListenerForSingleValueEvent有时不返回数据

时间:2022-05-06 22:56:36

When my application starts, I check the current user's details in my Firebase database (I'm storing it's uid for that).

当我的应用程序启动时,我在我的Firebase数据库中检查当前用户的详细信息(我将它存储为uid)。

I'm attaching addListenerForSingleValueEvent to user's ref to read it's data.

我将addListenerForSingleValueEvent附加到用户的ref读取数据。

My problem is that sometimes, it doesn't return any value, neither success nor failure.

我的问题是,有时它没有任何价值,既没有成功也没有失败。

Only clearing application's data solves it, but of course forces the user to login again.

只有清除应用程序的数据才能解决这个问题,但当然也会迫使用户再次登录。

I've read some posts at SO, but didn't find any solution.

我读过一些文章,但没有找到任何解决办法。

Here's my piece of code:

这是我的一段代码:

DatabaseReference newUser =    
FirebaseDatabase.getInstance().getReference("users/"+uid);
newUser.addListenerForSingleValueEvent(new ValueEventListener()     
    @Override
    public void onDataChange(DataSnapshot snapshot) {}

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

3 个解决方案

#1


5  

The recommendation I've seen for this is to call keepSynced(true) on the Query object.

我看到的建议是在查询对象上调用keepSynced(true)。

#2


0  

My guess is user/:id does not exist. Double-check your error output:

我的猜测是user/:id不存在。仔细检查你的错误输出:

DatabaseReference newUser = FirebaseDatabase.getInstance()
    .getReference("users")
    .child(uid)
    .addListenerForSingleValueEvent(new ValueEventListener()     
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            if (snapshot.exists()) {
                Log.i(TAG, snapshot.val());
            } else {
                Log.e(TAG, "Not found: " + uid);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e(TAG, databaseError.toString();
        }
    });

#3


-1  

I've been having the same problem for days and I finally fixed it.

我已经有同样的问题好几天了,我终于解决了。

Looks like my Api key was changed! And that's what was preventing addListenerForSingleValueEvent() from returning!
Go to Firebase Console, open your project, click the settings icon Firebase Android addListenerForSingleValueEvent有时不返回数据 and go to Project settings. There you'll see Web API Key. Take that and copy it to your google-services.json file in your project's directory. Here's a snippet from that file:

看起来我的Api键被修改了!这就是防止addListenerForSingleValueEvent()返回的原因!转到Firebase控制台,打开项目,单击settings图标,转到project settings。您将看到Web API Key。把它复制到你的google服务上。项目目录中的json文件。下面是该文件的一个片段:

  ...
  ],
  "api_key": [
    {
      "current_key": "<COPY Web Api Key HERE>"
    }
  ],
  ...

That's it. After replacing the value of "current_key" with the key from Firebase, it returned to work (previously it had some other key I think I generated at Google Console and it worked for months. Just a few days ago it stopped working)

就是这样。在从Firebase中替换了“current_key”的值之后,它返回了工作(以前它有其他一些我认为是在谷歌控制台生成的键,它工作了几个月。就在几天前,它停止了工作)

#1


5  

The recommendation I've seen for this is to call keepSynced(true) on the Query object.

我看到的建议是在查询对象上调用keepSynced(true)。

#2


0  

My guess is user/:id does not exist. Double-check your error output:

我的猜测是user/:id不存在。仔细检查你的错误输出:

DatabaseReference newUser = FirebaseDatabase.getInstance()
    .getReference("users")
    .child(uid)
    .addListenerForSingleValueEvent(new ValueEventListener()     
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            if (snapshot.exists()) {
                Log.i(TAG, snapshot.val());
            } else {
                Log.e(TAG, "Not found: " + uid);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e(TAG, databaseError.toString();
        }
    });

#3


-1  

I've been having the same problem for days and I finally fixed it.

我已经有同样的问题好几天了,我终于解决了。

Looks like my Api key was changed! And that's what was preventing addListenerForSingleValueEvent() from returning!
Go to Firebase Console, open your project, click the settings icon Firebase Android addListenerForSingleValueEvent有时不返回数据 and go to Project settings. There you'll see Web API Key. Take that and copy it to your google-services.json file in your project's directory. Here's a snippet from that file:

看起来我的Api键被修改了!这就是防止addListenerForSingleValueEvent()返回的原因!转到Firebase控制台,打开项目,单击settings图标,转到project settings。您将看到Web API Key。把它复制到你的google服务上。项目目录中的json文件。下面是该文件的一个片段:

  ...
  ],
  "api_key": [
    {
      "current_key": "<COPY Web Api Key HERE>"
    }
  ],
  ...

That's it. After replacing the value of "current_key" with the key from Firebase, it returned to work (previously it had some other key I think I generated at Google Console and it worked for months. Just a few days ago it stopped working)

就是这样。在从Firebase中替换了“current_key”的值之后,它返回了工作(以前它有其他一些我认为是在谷歌控制台生成的键,它工作了几个月。就在几天前,它停止了工作)