Firebase中的嵌套列表 [英] Nested Lists in Firebase

查看:121
本文介绍了Firebase中的嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图理解如何在Firebase中实现嵌套列表。



问题可以简化为:1对N消息传递系统,对于每个消息,维护已收到和阅读该消息的用户列表。

阅读Firebase中阵列的最佳实践。试图避免数组,因为我有同时写入,他们只是在这里似乎不是一个好的选择。

目前试图通过在每个消息下存储子树,每个子树都是在消息 X 上接收,阅读或以其他方式执行某个操作 X

 msgid0:{
authorID:uid0,
msg:消息文本,
ReceivedBy:{
uid1:true,
uid2:true
}
ReadBy:{
uid1:true






$ b问题:是否可以像这样放置一个嵌套的数据结构,直接到一个单一的对象?



我粗暴地尝试一下测试用例:
$ b $

  public class Message {
private Long authorID;
private String msg;
私人清单< String> receivedBy;
私人清单< String> readBy; (DataSnapshot i:dataSnapshot)


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

但是失败:
$ b


引起:com.fasterxml。 jackson.databind.JsonMappingException:
可以不反序列化java.util.ArrayList的实例,但不包括START_OBJECT
令牌
$ /
$ div class =h2_lin >解决方案

啊,只需要使用 Map 而不是 List p>

在我的例子中:

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

现在我明白了如何使用recursiv e结构与Firebase。猜猜大多数事情,这很容易 - 一旦你知道如何。


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

解决方案

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.

这篇关于Firebase中的嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆