André Nogueira
André Nogueira

Reputation: 3983

Cannot retrieve any values using equalTo() query Firebase

I'm building a chat app and this is what my Database looks like: Please, click here to see

So, I currently have a users node as well as a chats node.

The users node saves the last last login date and the chats Ids that the user is currently part of. In the chats node, it is saved the User Ids that make part of the conversation as well as the messages which have an Id, Content, Send Date, and User who sent.

Currently I am building the activity where it is displayed the conversations that the user is part of (using a recyclerview).

To be able to know what conversations the user is part of, instead of retrieving all conversations, I retrieve the chats ids that are in the user node and I put them all in an ArrayList<String>.

Here is a picture that helps you see what goes in the ArrayList

The code that retrieves those Ids and puts them in the ArrayList is this:

        db.addListenerForSingleValueEvent(object : ValueEventListener{
            override fun onCancelled(p0: DatabaseError) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun onDataChange(p0: DataSnapshot) {
                p0.children.forEach{
                    chatsId.add(it.key.toString()) // ChatsId is the ArrayList<String>
                }
                getChats2()
            }

        })

And this code works like a charm. The problem comes when I try to query the chats node just to retrieve the chat that has the value equal to the values retrieved in the code above. ** That code is implemented in the getsChat2() **:

 db = FirebaseDatabase.getInstance().getReference("chats")

        // For each chatId, queries the DB to find the chat reference
        chatsId.forEach {
            val query = db.equalTo(it)
            query.addValueEventListener(object : ValueEventListener{
                override fun onCancelled(p0: DatabaseError) {
                    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                }

                override fun onDataChange(p0: DataSnapshot) {
                    p0.children.forEach {it2->
                        println("Do something")
                        // Do something with the chat reference
                    }
                }

            })
        }

The problem is, even if there is a node with the same value as the chatsId variable, the println("Do Something) never actually prints.

Upvotes: 1

Views: 110

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598718

It's a bit hard to understand what you're trying to do, but my educated guess is that you're missing an orderByKey() call:

val query = db.orderByKey().equalTo(it)

When you don't call any orderBy...() method, the nodes are sorted on their priority. That's a left-over from the early days of the API that has very little practical use, so these days it mostly means that you must call orderBy...() before filtering.

Upvotes: 2

Related Questions