Firebase Android addListenerForSingleValueEvent有时不返回数据 [英] Firebase Android addListenerForSingleValueEvent sometimes not returning data

查看:85
本文介绍了Firebase Android addListenerForSingleValueEvent有时不返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我将addListenerForSingleValueEvent附加到用户的ref上以读取其数据.

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

我的问题是,有时它不返回任何值,无论成功还是失败.

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.

我在SO上阅读了一些帖子,但没有找到任何解决方案.

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

这是我的代码:

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

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

推荐答案

为此,我看到的建议是在Query对象上调用keepSynced(true).

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

这篇关于Firebase Android addListenerForSingleValueEvent有时不返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆