4

Trying to understand how to implement nested lists in Firebase.

Problem reducible to: a 1-to-N messaging system, where, for each message, you wish to maintain a list of users who have received, and read, that message.

Have read "Best Practices for Arrays in Firebase". Trying to avoid arrays, as I have simultaneous writes, and they just don't seem a good choice here.

Currently trying to achieve this by storing subtree's under each message, each subtree being a list of users who have received, read, or otherwise performed some action X on message Y

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

Question: Is it possible to put a nested datastructure like this, directly into a single object?

I am crudely trying a test case with the following:

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 fails with:

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

Mtl Dev
  • 1,604
  • 20
  • 29

1 Answers1

7

Ah, just needed to use Map instead of List.

In my case:

public class Message {
  private Long authorID;
  private String msg;
  private Map<String, Boolean> receivedBy;
  private Map<String, Boolean> readBy;
}

Now I understand how to use recursive structures with Firebase. Guess like most things, it is easy -- once you know how.

Mtl Dev
  • 1,604
  • 20
  • 29