android – Firebase addValueEventListener只能工作几个小时

前端之家收集整理的这篇文章主要介绍了android – Firebase addValueEventListener只能工作几个小时前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有人遇到过这个问题?我的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);

线条:

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

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

编辑:

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

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

在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身份验证令牌未正确刷新本身引起的,这本身是由Firebase项目中的潜在错误配置引起的.

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

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

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

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

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

原文链接:https://www.f2er.com/android/314131.html

猜你在找的Android相关文章