Firebase addValueEventListener只能工作几个小时 [英] Firebase addValueEventListener only working for a couple of hours
问题描述
ValueEventListener valueEventListener = new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
Log.e(TAG,onDataChange:Job found); (DataSnapshot jobSnapShot:dataSnapshot.getChildren()){
Log.e(TAG,onDataChange:Job +1);
Job job = jobSnapShot.getValue(Job.class);
//将ID添加到作业
job.setId(dataSnapshot.getKey());
//设置作业
arrayList.add(job);
subscriber.onNext(job);
$ b @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似乎并没有在几个小时后添加监听器。解决这个问题的唯一方法是注销并重新登录。
编辑:
我的验证状态监听器代码:
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();
//用户已登录
Log.e(TAG,onAuthStateChanged:以+ id登录);
//登录时启动loginActivity
loginActivity.onLoginSuccess(id);
} else {
//用户没有登录
Log.e(标签,onAuthStateChanged:签出);
//用户可能已经注销。完成loginActivity并启动登录屏幕
}
}
};
此问题是由于Firebase身份验证令牌不能自我刷新正确的,这本身是由您的Firebase项目中的基础配置错误造成的。
您可以通过在签名用户后调用以下代码段来判断令牌刷新是否失败:
FirebaseUser user = mAuth.getCurrentUser(); // mAuth是您当前的firebase身份验证实例
user.getToken(true).addOnCompleteListener(this,new OnCompleteListener&GetTokenResult>(){
@Override $ b $ public void onComplete(@NonNull Task< GetTokenResult>任务){
if(task.isSuccessful()){
Log.d(TAG,token =+ task.getResult()。getToken());
} else {
Log.e(TAG,exception =+ task.getException()。toString());
}
}
});
(如果有问题,您将得到一个异常)。
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);
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.
EDIT:
My auth state listener code:
firebaseAccount = getFirebaseAccount();
firebaseAccount.getAuth().addAuthStateListener(firebaseAccount.getAuthListener());
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
}
}
};
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).
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屋!