Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group of users in firebase

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?

like image 290
JamieB Avatar asked Jul 05 '16 21:07

JamieB


People also ask

How are users counted in Firebase?

Sessions - The number of times a user started the app in the given day. Multiple times count as multiple sessions. Active Users - The number of users who interacted with the app by starting at least one session. Multiple sessions only count as a single user.

Are users in Firebase unique?

Firebase users have a fixed set of basic properties—a unique ID, a primary email address, a name and a photo URL—stored in the project's user database, that can be updated by the user (iOS, Android, web).

Can you collaborate on Firebase?

Adding members to your Firebase project allows you to collaborate across your team. You can assign each member a role based on the level of access the member needs for your project.


1 Answers

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
      }
    },
    ...
  }
}
like image 170
Chintan Soni Avatar answered Oct 14 '22 12:10

Chintan Soni