I have been dealing with a really weird and frustrating problem for almost a year now. I have an Android app that (unfortunately) uses Firebase Realtime database. While some queries are stable, meaning they return the correct number of matching items, others are not stable. They just don't see the latest version of the database and thus return a wrong number of matching items. Here is for example a discussion I started 10 months ago, which has not been resolved (Firebase Query in Android returns random number of items altough multiple items match the query).
The problem with the unstable queries is, that they somehow query an internal representation of the database that is not always the current version. When I make some specific changes on the database, they don't see them.
Extremely weirdly, there are some countermeasures that sometimes help, like for example
- Execute the same query multiple times (about 20 times) within one second
- Execute a stable query on the same database
- Execute a generic query that just returns every item of the node before executing the real query
- Querying on another attribute.
Strangely, for different unstable queries, different countermeasures may or may not work and there is no rationale behind that.
Once a unstable query has "updated its internal view" on the real database, it always returns the correct number of matching units, if the real database itself does not change.
I have already contacted the Firebase Google team but they could not help so far. What I can definitely rule out, is that the code of the query (Java) itself is wrong. My code is correct but somehow there seem to be some strange conflicts with something else.
My question to you is whether you have also experienced something similar and could think about a reason as to why this is happening or if you can think about a more universal coutermeasure. Especially I would like to know if there is a way to tell the Firebase handling threads in Android to update their "internal view" on the firebase before doing a query. This should not be confused with something like livedata. My problem is a fundamental one dealing with how the queries are exactly executed in Android.
Strangely, in my app I also have stable queries that always return the correct number of matching units. I'd say about 70 % are stable queries that always work with the current view of the real database. Unfortunately I have about 5 unstable queries and I am becoming more and more "anxious" of using Firebase as it provides an unreliable and random database querying system (and I am already quite frustrated as I have never seen anything like this before).
Does anyone of you have and idea what else I could do? I'll highly appreciate every comment.
Edit: Here is an example of an unstable Firebase query:
DatabaseReference rootRef_Firebase = FirebaseDatabase.getInstance(FIREBASE_URL).getReference();
rootRef_Firebase
.child("Orders")
.orderByChild("oderID")
.equalTo(2)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren()) {
String itemName="";
if (ds.child("name").getValue(String.class)!=null) {
itemName= ds.child("name").getValue(String.class);
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
Executed on this database:
"Orders": {
"table_1_order_7_date_29-04-22_time_18-17-49": {
"comment_Text": "",
"name": "Order_A",
"orderDate": "18:17 (29.04.22)",
"orderDateInMilliseconds": 1651249069304,
"orderID": 2,
"orderOtherInfo": "",
"orderStatus": "ordered",
"quantity": 1,
"tableNumber": 1
},
"table_1_order_8_date_29-04-22_time_18-17-52": {
"comment_Text": "",
"name": "Order_B",
"orderDate": "18:17 (29.04.22)",
"orderDateInMilliseconds": 1651249072115,
"orderID": 2,
"orderOtherInfo": "",
"orderStatus": "prepared",
"quantity": 1,
"tableNumber": 1
},
"table_1_order_9_date_29-04-22_time_18-17-54": {
"comment_Text": "",
"name": "Order_C",
"orderDate": "18:17 (29.04.22)",
"orderDateInMilliseconds": 1651249074747,
"orderID": 9,
"orderOtherInfo": "",
"orderStatus": "prepared",
"quantity": 1,
"tableNumber": 1
}
},
Although there are 2 matching items, the unreliable service provided by the Firebase queries don't always return 2 items. Sometimes they don't return anything, sometimes only 1 item, sometimes 2.
Reminder: Unfortunately I am having this issue for more than 1 year and already spent a huge amount of time trying to solve it without success. Do you have any advice?