Firebase中的用户组

Firebase中的用户组

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

问题描述

User 被定义为:

  public class User { 
私人字符串电子邮件;
private String uid;
私人列表< Group>组;
$ b $ public User(String email,String uid){
this.email = email;
this.uid = uid;
this.groups = new ArrayList<>();

$ b $ public User(){}
$ b public User(String email,String uid,ArrayList< Group> groups){
this.email =电子邮件;
this.uid = uid;
this.groups = groups;
}

public String getEmail(){
return email;
}

public String getUid(){
return uid;
}

公共列表< Group> getGroups(){
返回组;
}
$ b $ public void addGroup(Group group){
if(this.groups == null){
this.groups = new ArrayList<> ;
}
this.groups.add(group);

$ b

群组

  public class Group {
private List< User>会员;

私人Group(){
}

公共群组(列表<用户>用户){
this.memberList = users;
}

public void addMember(User member){
this.memberList.add(member);
}

public List< User> getMemberList(){
return memberList;






当试图保存到firebase时,错误:

$ p $ java.lang.NoClassDefFoundError:com / fasterxml / jackson / databind / JsonMappingException
at com.fasterxml .jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:611)

是问题是使用循环引用还是Firebase无法以这种方式存储数据?解决方案

是的我认为问题是循环参考。在有循环引用的地方,总是有序列化的问题。



我们可以看到用户和组之间有双向关系。根据,您可以改进结构:

  {
users:{
user1:{
name:User 1,
groups:{
group1:true,
group2:true,
group3:true
},
user2 :{
name:User 2,
groups:{
group2:true,
group3:true
}



group{b $ bgroup1:{
name:Group 1,
members:{
user1:true
},
group2:{
name:Group 2,
members :{
user1:true,
user2:true
},
group3:{
name:Group 3,
members:{
user1:true,
user2:true
}
},
...
}
}


A User is defined as:

public class User {
  private String email;
  private String uid;
  private List<Group> groups;

  public User(String email, String uid) {
    this.email = email;
    this.uid = uid;
    this.groups = new ArrayList<>();
  }

  public User() {}

  public User(String email, String uid, ArrayList<Group> groups) {
    this.email = email;
    this.uid = uid;
    this.groups = groups;
  }

  public String getEmail() {
    return email;
  }

  public String getUid() {
    return uid;
  }

  public List<Group> getGroups() {
    return groups;
  }

  public void addGroup(Group group) {
    if (this.groups == null) {
      this.groups = new ArrayList<>();
    }
    this.groups.add(group);
  }
}

Group is defined as:

public class Group {
  private List<User> memberList;

  private Group() {
  }

  public Group(List<User> users) {
    this.memberList = users;
  }

  public void addMember(User member) {
    this.memberList.add(member);
  }

  public List<User> getMemberList() {
    return memberList;
  }
}

When attempting to save to firebase, this gives a runtime error of:

java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JsonMappingException
                                                                             at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:611)

Is the issue to do with circular references or is Firebase not able to store data in this way?

解决方案

Yes I think the issue is with Circular reference. Where there is Circular reference, there is always issue with serializing it.

As we can see you have two - way relationship between Users and Groups. According to official documentation you can improve the structure as:

{
  "users": {
    "user1": {
      "name": "User 1",
      "groups": {
         "group1": true,
         "group2": true,
         "group3": true
      },
    "user2": {
      "name": "User 2",
      "groups": {
         "group2": true,
         "group3": true
      }
    },
    ...
  },
  "groups": {
    "group1": {
      "name": "Group 1",
      "members": {
        "user1": true
      },
    "group2": {
      "name": "Group 2",
      "members": {
        "user1": true,
        "user2": true
      },
    "group3": {
      "name": "Group 3",
      "members": {
        "user1": true,
        "user2": true
      }
    },
    ...
  }
}

这篇关于Firebase中的用户组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:36