Firebase addValueEventListener 只能工作几个小时 [英] Firebase addValueEventListener only working for a couple of hours

查看:21
本文介绍了Firebase addValueEventListener 只能工作几个小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人遇到过这个问题吗?我的 firebase 代码基本上只能工作几个小时(功能齐全),然后当我再次尝试时它不再工作了.请参阅下面的代码以了解我如何调用它:

has anyone experienced this issue? My firebase code only works for basically a couple of hours (fully functional and all), and then when I try again it doesn't work anymore. See below code for how I am calling it:

        ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.e(TAG, "onDataChange: Job found");
                for (DataSnapshot jobSnapShot : dataSnapshot.getChildren()) {
                    Log.e(TAG, "onDataChange: Job +1");
                    Job job = jobSnapShot.getValue(Job.class);
                    // Add the ID into the job
                    job.setId(dataSnapshot.getKey());

                    // Set the job
                    arrayList.add(job);
                    subscriber.onNext(job);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.e(TAG, "onCancelled: " + databaseError.getMessage());
            }
        };
        Log.e(TAG, "call: id:" + userId + ", reference:" + FirebaseDatabase.getInstance().getReference().toString());
        Log.e(TAG, "call: Calling Jobs...");
        FirebaseDatabase.getInstance()
                .getReference()
                .child(context.getString(R.string.firebase_jobs))
                .child(userId).
                addValueEventListener(valueEventListener);

行:

    Log.e(TAG, "call: id:" + userId + ", reference:" + FirebaseDatabase.getInstance().getReference().toString());
    Log.e(TAG, "call: Calling Jobs...");

每次都执行.UserId 和 getReference 返回正确的值.然而,基本上几个小时后, addValueEventListener 似乎并没有添加侦听器.解决此问题的唯一方法是注销并重新登录.

Execute every single time. UserId and getReference returns correct values. However the addValueEventListener does not seem to be adding the listener after basically several hours later. The only way to fix this is to log off and log back on.

我的身份验证状态侦听器代码:

My auth state listener code:

firebaseAccount = getFirebaseAccount();
firebaseAccount.getAuth().addAuthStateListener(firebaseAccount.getAuthListener());

在 firebaseAccount 中:

In firebaseAccount:

public FirebaseAuth.AuthStateListener getAuthListener() {
    return authStateListener;
}

FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
        if (firebaseUser != null) {
            String id = firebaseUser.getUid();
            // User is signed in
            Log.e(TAG, "onAuthStateChanged: Signed in as " + id);
            // Start loginActivity when signed in
            loginActivity.onLoginSuccess(id);
        } else {
            // User is not signed in
            Log.e(TAG, "onAuthStateChanged: Signed out");

            // User probably logged out. Finish the loginActivity and launch the login screen
        }
    }
};

推荐答案

此问题是由 Firebase Auth 令牌未正确刷新自身引起的,而这本身是由 Firebase 项目中的底层错误配置引起的.

This issue is caused by Firebase Auth tokens not refreshing themselves properly, which itself is caused by an underlying misconfiguration in your Firebase project.

您可以通过在用户登录后调用以下代码段来判断令牌刷新是否失败:

You can tell if the token refresh is failing by calling the following snippet after you sign a user in:

FirebaseUser user = mAuth.getCurrentUser(); // mAuth is your current firebase auth instance
user.getToken(true).addOnCompleteListener(this, new OnCompleteListener<GetTokenResult>() {
    @Override
    public void onComplete(@NonNull Task<GetTokenResult> task) {
        if (task.isSuccessful()) {
            Log.d(TAG, "token=" + task.getResult().getToken());
        } else {
            Log.e(TAG, "exception=" +task.getException().toString());
        }
    }
});

(如果有问题,你会得到一个例外).

(If there is an issue, you will get an exception).

您可以遵循我们拥有的本指南与 Firebase 团队一起对可能导致此问题的任何配置问题进行故障排除和修复.

You can follow this guide that we have put together in the Firebase team to troubleshoot and fix any issues with configuration that can cause this problem.

上述步骤应该是对该问题的永久修复,但是,我们也在努力实施一种自动检测错误配置的方法,并为您透明地修复它们.对于由此可能给您造成的任何问题,我们深表歉意.

The above steps should be a permanent fix for the issue, however, we are also working hard to implement a way of automatically detecting misconfigurations and fixing them transparently for you. Apologies for any problem this may have caused you.

这篇关于Firebase addValueEventListener 只能工作几个小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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