Java – Nested lists in Firebase

Nested lists in Firebase… here is a solution to the problem.

Nested lists in Firebase

Try to understand how to implement nested lists in Firebase.

The problem can be simplified to a 1:N messaging system, and for each message, you want to maintain a list of users who have received and read the message.

Read “Best Practices for Arrays in Firebase” Try to avoid using arrays as I have synchronous writes and they don’t seem like a good choice here.

An attempt is being made to do this by storing subtrees under each message, each of which is a list of users who have received, read, or otherwise performed some action on the message X is

"msgid0" : {
        "authorID": "uid0",
        "msg"     : "message text",
        "ReceivedBy": {
           uid1 : true,
           uid2 : true
         }
        "ReadBy" :    {
           uid1 : true
        }
}

Question: Is it possible to put such nested data structures directly into a single object?

I’m trying to test with the following in a rough attempt :

public class Message {
  private Long authorID;
  private String msg;
  private List<String> receivedBy;
  private List<String> readBy;
}

ref.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    for(DataSnapshot i: dataSnapshot.getChildren()){
      Message_FB msg = i.getValue(Message_FB.class);
    }
  }
}

But it failed :

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can
not deserialize instance of java.util.ArrayList out of START_OBJECT
token

Related Problems and Solutions