Home > Net >  I want to set the firebase real-time database or conditional statement
I want to set the firebase real-time database or conditional statement

Time:10-28

I'm making a chat app. Person A sends a message to B, which is clearly visible when viewing the chat room on A's device. But when I go to B's device and look at the chat window, I can't see the message sent by A. I know what the problem is, but I don't know how to fix it. please I need help.

My database has the following structure:

"chatrooms" : {
    "-Mn0ZDpdWsOb8XFFTYmi" : {
      "comments" : {
        "-Mn0ZG0UoMY81i9sf6nN" : {
          "message" : "hi",
          "timestamp" : 1635335082263,
          "uid" : "QTC8Msw1n2aUF8mDbTCUNk5k7sH3"
        }
      },
      "users" : {
        "itemName" : "goods",
        "sellerUid" : "vzTQG0Fb3DgsVKGgGj0R23uL6Hx2",
        "uid" : "QTC8Msw1n2aUF8mDbTCUNk5k7sH3"
      }
    }

It's a problematic code:

FirebaseDatabase.getInstance().getReference().child("chatrooms")
        .orderByChild("users/uid").equalTo(uid).addListenerForSingleValueEvent(new ValueEventListener(){
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        chatModels.clear();
        for (DataSnapshot item : snapshot.getChildren()){
            chatModels.add(item.getValue(ChatModel1.class));
        }
        notifyDataSetChanged();
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {

    }
});

============

FirebaseDatabase.getInstance().getReference().child("chatrooms")
        .orderByChild("users/sellerUid").equalTo(uid).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        chatModels.clear();
        for (DataSnapshot item : snapshot.getChildren()){
            chatModels.add(item.getValue(ChatModel1.class));
        }
        notifyDataSetChanged();
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {

    }
});

This shows A's message on B's device, which is far from solving the problem.

so, What I want is: FirebaseDatabase.getInstance().getReference().child("chatrooms") .orderByChild("users/uid").equalTo(uid).addListenerForSingleValueEvent

In this part, we want not only users/uid but also users/cellerUid to be searched and the euqalTo(udi) condition is executed.

CodePudding user response:

Queries in Firebase operate on a single flat node, where the value to order/filter on must be at a fixed path under each direct child node. There is no way to search across two branches of the tree in Firebase.

As usual when working with NoSQL databases, you will have to modify/extend your data model to allow the use case. Here it seems like you'll want a new top-level node that maps UIDs (from the comments) to the chat rooms that they are part of:

chatRoomsByUID: {
  "QTC8Msw1n2aUF8mDbTCUNk5k7sH3": {
    "-Mn0ZDpdWsOb8XFFTYmi": true
  }
}

This sort of "index" makes it then easy to look up the chat rooms for a user.

I'd also recommend reading my answer to:

  • Related