0

I am using the Firebas Realtime Database for Android and I would like to read data from my database from the node ("Table") "Ratings". Alls entries in that node are from the type Item_FirebaseDB_Rating and all of them have a variable called orderID. Now I would like to query the Firebase Database once to check, whether there is an entry in it with a specific orderID. This should just be a single check, whenever I call the method checkIfItemAlreadyRated_FirebaseDB and it should not continuously check for updates (as many other Firebase Database queries do).

For that I use the following method:

 public static boolean checkIfItemAlreadyRated_FirebaseDB (int orderID) {
        boolean result = false;
// Not the whole name of the database is given in the URL for privacy reasons.
        DatabaseReference rootRef = FirebaseDatabase.getInstance("https://...firebasedatabase.app").getReference();


        ValueEventListener ratingListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                Item_FirebaseDB_Rating rating = dataSnapshot.getValue(Item_FirebaseDB_Rating.class);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                // Getting Post failed, log a message
                Log.e("LogTag", "loadPost:onCancelled", databaseError.toException());
            }
        };

        Query query = FirebaseDatabase.getInstance("https://...firebasedatabase.app").getReference("Ratings")
                .orderByChild("orderID").equalTo(orderID);
        query.addListenerForSingleValueEvent(ratingListener);

        if(query != null) {
            result = true;
        }


        return result;
    }

I have 2 question on that:

  1. When I call this method, I get an error that I don't understand " com.google.firebase.database.DatabaseException: Class com.example.td.bapp.Item_FirebaseDB_Rating does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped."
  2. I define a query to get the Item_FirebaseDB_Rating rating from the database. Now my question is, how can I get this object out of the Listener and use it in the other part of the method? So how can I get the results of the query? There should be something like query.getResults() or similar. But I could not find it.
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
VanessaF
  • 515
  • 11
  • 36
  • There is no way you can return the `result` object as a result of a method. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this article, [How to read data from Firebase Realtime Database using get()?](https://medium.com/firebase-tips-tricks/how-to-read-data-from-firebase-realtime-database-using-get-269ef3e179c5). – Alex Mamo Dec 04 '21 at 09:55
  • @AlexMamo: Thanks for your comment. Reading the official Firebase documentation I think it says that you can return the Java object "Calling getValue() on a snapshot returns the Java object representation of the data. " (https://firebase.google.com/docs/database/android/read-and-write#java_4). So actually I would like to get the result of the query in any form (ideally the Java objects as it is stated in the documentation). – VanessaF Dec 04 '21 at 10:00
  • @AlexMamo: So is there no way to query the Firebase Database immediately and check whether a certain entry exists? Thanks for your link to the article. Unfortunately I don't use Kotlin so I can't really work with that. – VanessaF Dec 04 '21 at 10:07
  • Sure you can check if a specific [child already exists](https://stackoverflow.com/questions/47893328/checking-if-a-particular-value-exists-in-the-firebase-database). – Alex Mamo Dec 04 '21 at 10:10
  • @AlexMamo: Thanks for the comment. But how can I get the result of a query like I did with `Query query = FirebaseDatabase.getInstance("https://...app").getReference("Ratings") .orderByChild("orderID").equalTo(orderID);`. I have a query and need the results of it. If I understood you correctly, I can't get the Java objects from this query, but can I at least get the String information as in SQL from that query? If I can't get back the Java object as you stated, what is the offical documentation then referring to when saying "returns the Java object representation of the data" – VanessaF Dec 04 '21 at 10:37
  • You may find this [question](https://stackoverflow.com/questions/57330766/why-does-my-function-that-calls-an-api-return-an-empty-or-null-value) helpful – Tyler V Dec 04 '21 at 14:48

0 Answers0