Firebase addValueEventListener只能工作几个小时

时间:2022-02-21 01:25:46

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:

有没有人遇到过这个问题?我的firebase代码只能工作几个小时(完全功能和全部),然后当我再试一次它不再起作用。请参阅下面的代码,了解我如何调用它:

        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);

The lines:

线条:

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

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.

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

EDIT:

编辑:

My auth state listener code:

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

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

In firebaseAccount:

在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
        }
    }
};

1 个解决方案

#1


15  

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

此问题是由Firebase身份验证令牌未正确刷新本身引起的,这本身是由Firebase项目中的潜在错误配置引起的。

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).

(如果出现问题,您将获得例外)。

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.

您可以按照我们在Firebase小组中放在一起的本指南进行故障排除并修复可能导致此问题的配置问题。

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.

上述步骤应该是问题的永久性解决方案,但是,我们也在努力实现一种自动检测错误配置并透明地为您修复它们的方法。对此可能导致您的任何问题表示歉意。

#1


15  

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

此问题是由Firebase身份验证令牌未正确刷新本身引起的,这本身是由Firebase项目中的潜在错误配置引起的。

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).

(如果出现问题,您将获得例外)。

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.

您可以按照我们在Firebase小组中放在一起的本指南进行故障排除并修复可能导致此问题的配置问题。

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.

上述步骤应该是问题的永久性解决方案,但是,我们也在努力实现一种自动检测错误配置并透明地为您修复它们的方法。对此可能导致您的任何问题表示歉意。