简体   繁体   English

Firebase实时数据库不会更新值

[英]Firebase Realtime Database doesn't update values

I have been writing an app using firebase database and for the last two days, it doesn't work. 我一直在使用firebase数据库编写应用程序,在过去的两天里,它不起作用。 What I mean by this is that I have a connection to the database, but when I try to read information by setting addListenerForSingleValueEvent it doesn't run the ValueEventListener even though I updateChildren . 我的意思是,我要与数据库的连接,但是当我试图通过设置读取信息addListenerForSingleValueEvent它不运行ValueEventListener即使我updateChildren

        DatabaseReference mBlaze = FirebaseUtil.getDatabase(PLACES);
        mBlaze.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot snapShot : dataSnapshot.getChildren()) {
                    System.out.println("Hello");
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                System.out.println("Error");
            }
        });
        long sTime = System.currentTimeMillis();
        while (!triggers[0])
            if (System.currentTimeMillis() - sTime > 5000)

I have a trigger value in the database used just to run the addListenerForSingleValueEvent . 我在数据库中有一个触发器值,用于运行addListenerForSingleValueEvent

If I try to run this on my device\\emulator, it won't print out the message "Hello", nor will it print out the message "Error". 如果我尝试在我的设备\\模拟器上运行它,它将不会打印出消息“Hello”,也不会打印出消息“Error”。
The reason this is a problem is that I am trying to verify a code against the firebase database. 这是一个问题的原因是我试图验证firebase数据库的代码。 I give it a 5 second time window for the the value to change, meaning the code was found, and if after 5 second the value of triggers[0] doesn't change, it means the code isn't valid. 我给它一个5秒的时间窗口来改变值,这意味着找到了代码,如果在5秒后triggers[0]的值没有改变,则意味着代码无效。

During my attempts to reduce the code I have stumble upon the setLogLevel which after I used it with Level.DEBUG , I have found that this line appears over and over again: 在我尝试减少代码的setLogLevel中,我Level.DEBUG发现了setLogLevel ,在我将它与Level.DEBUG一起使用Level.DEBUG ,我发现这一行一遍又一遍地出现:

Error fetching token: The user's credential is no longer valid. 获取令牌时出错:用户的凭据不再有效。 The user must sign in again. 用户必须再次登录。

As I stated later on, when I was trying to reduce the code, I found the setLogLevel method, which is used with Level.DEBUG , which I then used to discover the error: 正如我后面所说,当我试图减少代码时,我找到了setLogLevel方法,它与Level.DEBUG一起Level.DEBUG ,然后我用它来发现错误:

Error fetching token: The user's credential is no longer valid. 获取令牌时出错:用户的凭据不再有效。 The user must sign in again. 用户必须再次登录。

Which after some snooping around I discovered, that due to the Firebase-Auth mechanism, which grants a user a token for 24 hours, and then de-auth's it, causes the failure to reach the sever. 经过一些窥探,我发现,由于Firebase-Auth机制,它为用户授予了24小时的令牌,然后取消授权,导致无法到达服务器。
So the solution would be to implement either a perma-login [permanent login] feature, or every time the user destroys the application or starts it from scratch, check if there is a user logged in using the getCurrentUser() method in FirebaseAuth , and if there is one, log out. 所以解决方案是实现perma-login [永久登录]功能,或者每次用户销毁应用程序或从头开始,检查是否有用户使用FirebaseAuthgetCurrentUser()方法登录,并且如果有,请退出。

TL;DR: TL; DR:
Make sure you don't have a user signed in for more than 24 hours, and if you do, disconnect them and then reconnect them. 确保您没有用户登录超过24小时,如果您已登录,请断开连接,然后重新连接。

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

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