1

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?

VanessaF
  • 515
  • 11
  • 36
  • I think you should reach Firebase support. – Alex Mamo Dec 18 '22 at 07:11
  • @AlexMamo: Thanks Alex for your comment. I already contacted them several times and had E-Mail conversations during 3 months with exchange of more than 10 E-Mails. I even send them a self-made video where I clearly show the unstable behaviour of firebase. They could not help. This is why I would like to as others whether they have any idea why this is happening or any general work around solutions for that problem that I have been facing know for more than half a year. Unfortunately my question is closed here. Do you know any other Firebase community that might be helpful for this problem? – VanessaF Dec 18 '22 at 09:39
  • @AlexMamo: Is there a way to tell the Firebase queries to update their "internal view" of the real database or to tell them to query on the current realtime database? The fundamental problem is that the unstable queries use old versions of the database for quering, thus they don't see changes made in the database. As mentioned in my question, I found different inconsistant workarounds that sometimes work. But I could not find a general workaround for all unstable queries that always work. I have to tell the Firebaes queries to use the current version of the database – VanessaF Dec 18 '22 at 09:44
  • I don't know any other place, and I'm sorry to hear that. But I did not encounter such behavior in years. – Alex Mamo Dec 18 '22 at 11:08
  • @AlexMamo: Thanks for your comment. Do you know how to "force" the Firebase queries to use the current database and not old versions of it? So they should querry only on the current version. Can I somehow work with a timestamp in the queries or version arguments? – VanessaF Dec 18 '22 at 11:56
  • I'm not sure I understand, what "use the current database and not old versions of it" means. In the Realtime Database, if you have multiple databases, you can distinguish them through their URL using `FirebaseAuth.getInstance("https://...")`, right? – Alex Mamo Dec 19 '22 at 07:17
  • @AlexMamo: Thanks Alex for your answer. The problem with the unstable queries is, that they seem to use old versions of the realtime database. So If I adjust anything in the database, they don't see it and they query the old version of the database, that is not the current version. It is the version how the current version looked maybe some minutes before. As written in my question, somehow the unstable queries use old internal versions of the database. Sometimes doing one of the couter measures listed above helps. – VanessaF Dec 20 '22 at 21:26
  • But I would like to know if there is away to tell the queries to use the curent version of the database. Maybe the unstable queries have a wrong time stamp and use some internally logged versions of the database. Thus, they become unstable, as they don't return every matching item of the current database but seem to query on some strange old version of it. – VanessaF Dec 20 '22 at 21:32
  • Maybe it would be an idea to transfer all the data from "the old databases" into newer databases. – Alex Mamo Dec 21 '22 at 06:14
  • How can I do that? The old database does not physically exist. These are just imaginary databases, that the unstable queries use. Those databases often don't see the current changes. The unstable queries use them for quering and this is why they don't return all matching items from the real current database, as the "old databases" are just internal views of the unstable queries. I would like to tell the firebase queries, to only query on the latest database but they don't do that. Is there a way to update the internal views of the firebase queries before running the query? – VanessaF Dec 21 '22 at 21:37
  • @AlexMamo: Any comments to my last comment about how to "force" the Firebase queries to use the current database and not old imaginary views of it as the unstable queries are doing? – VanessaF Dec 23 '22 at 21:18
  • I'm sorry, no other thoughts. – Alex Mamo Dec 24 '22 at 07:42
  • @AlexMamo: But what else can I do? I already spent so much time on it and it's so extremely frustrating (I have never experienced anything like this in my whole programming life and could never imagine such a strange problem). What could be a possible explanation? Maybe there is a conflict with another library that I am using? Can I get the timestamp of the database when doing the query? Meaning that the query returns some metadata about the querried Firebase database which includes a timestamp of the latest update? – VanessaF Dec 24 '22 at 09:31
  • That can be possible, a conflict somewhere. You have to investigate that. Regarding a timestamp, you should track that. – Alex Mamo Dec 25 '22 at 08:25
  • @AlexMamo: Thanks for your answer. You wrote "That can be possible, a conflict somewhere. You have to investigate that." --> What would you advice me to do in order to track that? Further you wrote "Regarding a timestamp, you should track that." --> How can I do that? Is there a way to get the timestamp of the returned results of a Firebase Realtime Database query? Meaning that the query should tell on which database version it has executed its query – VanessaF Dec 26 '22 at 21:40
  • To investigate, you should isolate the database from other operations and see if it behaves the same. Regarding a timestamp, you cannot get the timestamp of the returned results of a Firebase Realtime Database query, unless you write some code for that. – Alex Mamo Dec 27 '22 at 07:22
  • @AlexMamo: Thanks for your comment. You wrote "To investigate, you should isolate the database from other operations and see if it behaves the same." I did that but it always behaves like this. I even just used an a fragment with just one operation (database query) and it was still unstable. Somehow the connection of the app to the firebase is rotten altough I use the correct google service file. There might a conflict with an other library I am using but I haven't figured it out altough having spent 100 hours on it. – VanessaF Dec 27 '22 at 11:02
  • Thanks Alex for your comment. You wrote "Regarding a timestamp, you cannot get the timestamp of the returned results of a Firebase Realtime Database query, unless you write some code for that." --> How can I write some code for that? For me it is quite important to see which version of the database is being queried because I have the impression that the unstable queries use old versions of the database (might even be some internal represenations of the database) – VanessaF Dec 27 '22 at 11:03
  • @AlexMamo: Thanks for your comments Alex. Any comments to my last 2 comments . One about how to investigate possible conflicts of firebase with other libraries and the second about how to implement you suggested approach of getting the timestamp of the firebase realtime database that is being queried? I'll highly appreciate every further comment from you. – VanessaF Dec 28 '22 at 15:14

0 Answers0