Firebase Android addListenerForSingleValueEvent sometimes not returning data
11,433
Solution 1
The recommendation I've seen for this is to call keepSynced(true)
on the Query
object.
Solution 2
Please try like this. If you can't solver by this, send share me source code for this part. ...
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("users").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
final HashMap<String, Object> dataMap = (HashMap<String, Object>) dataSnapshot.getValue();
for (String key : dataMap.keySet()){
Object data = dataMap.get(key);
try{
HashMap<String, Object> moneyData = (HashMap<String, Object>) data;
Money money = new Money(("", "");
}catch (ClassCastException cce){
try{
name = String.valueOf(dataMap.get("username"));
email = String.valueOf(dataMap.get("useremail"));
}catch (ClassCastException cce2){
}
}
}
// do something
}else {
// when empty data
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Author by
Adi
Updated on June 05, 2022Comments
-
Adi about 2 years
When my application starts, I check the current user's details in my Firebase database (I'm storing it's uid for that).
I'm attaching addListenerForSingleValueEvent to user's ref to read it's data.
My problem is that sometimes, it doesn't return any value, neither success nor failure.
Only clearing application's data solves it, but of course forces the user to login again.
I've read some posts at SO, but didn't find any solution.
Here's my piece of code:
DatabaseReference newUser = FirebaseDatabase.getInstance().getReference("users/"+uid); newUser.addListenerForSingleValueEvent(new ValueEventListener() @Override public void onDataChange(DataSnapshot snapshot) {} @Override public void onCancelled(DatabaseError databaseError) {} });
-
Frank van Puffelen about 8 yearsQuick question: are you using authentication? Are you accessing multiple databases in the same app (or have you previously used the same app to access a different database)?
-
Adi about 8 yearsYes I'm using authentication. I'm only access one database. BTW, even restarting the device didn't solve it.
-
Adi about 8 yearsThat problem has started only after I've start using the new SDK.
-
andre almost 8 yearsI can reproduce this problem always in combination with the solution of this Question: stackoverflow.com/questions/31670234/… So if I create a Value listener, which contains a single value listener, the single value listener never returns any data so everything freezes. Any solutions for this @FrankvanPuffelen? Do I do something wrong? Open Source project to reproduce, on PM if you like.
-
Alejandro Morán over 7 yearsI had the same problem today, I temporary fixed it by doing a logout of the current user, even when I deleted it from the database it was still available in the app
-
Pnemonic over 7 yearsI too had this problem. Call the firebase.authWithCustomToken BEFORE firebase.addListenerForSingleValueEvent
-
-
Nikunj Patel over 6 yearsI have same issue with addListenerForSingleValueEvent, is that first time when application was launch, its call properly but next time when i come back on application, its not call any override method. i tried keepsynced and its work now.
-
Nikunj Patel over 6 yearsbut fact is there are issue with New SDK