1
votes

I have a chat app and I'm trying to pull down the chat groups that the logged in user is part of. I'm struggling to get down the right groups. Here is my firebase structure. All the members are under the "members" child inside a group. I'm displaying the groups in a recyclerview that works fine. This has of course work for any user that logs in and a user can be part of many different groups. I hope you get the idea and can help me. Thanks

My code:

private FirebaseDatabase mDatabase;
private DatabaseReference mDatabaseReference;
private FirebaseAuth mAuth;
private FirebaseUser mUser;
private String projectId;
private List<DataService> grouplist;
private RecyclerAdapter recyclerAdapter;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mAuth = FirebaseAuth.getInstance();
    mUser = mAuth.getCurrentUser();
    mDatabaseReference = mDatabase.getReference().child("groups");

    mDatabase = FirebaseDatabase.getInstance();
    mDatabaseReference.keepSynced(true);

    grouplist = new ArrayList<>();
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
}

protected void onStart() {
    super.onStart();
    grouplist.clear();

        mDatabaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            DataService groupData = dataSnapshot.getValue(DataService.class);

            grouplist.add(groupData);

            recyclerAdapter = new RecyclerAdapter(Main.this, grouplist);
            recyclerView.setAdapter(recyclerAdapter);
            recyclerAdapter.notifyDataSetChanged();

        }


        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

DataService.java

public class DataService {
public String subject;
public String projectName;
public String endDate;
public String members;
public String userid;
public String creationDate;
public String dropbox;
public String evernote;
public String googleDocs;

public DataService() {
}


public DataService(String subject, String projectName, String endDate, String members, String userid, String creationDate, String dropbox, String evernote, String googleDocs) {
    this.subject = subject;
    this.projectName = projectName;
    this.endDate = endDate;
    this.members = members;
    this.userid = userid;
    this.creationDate = creationDate;
    this.dropbox = dropbox;
    this.evernote = evernote;
    this.googleDocs = googleDocs;
}

public String getCreationDate() {
    return creationDate;
}

public void setCreationDate(String creationDate) {
    this.creationDate = creationDate;
}

public String getDropbox() {
    return dropbox;
}

public void setDropbox(String dropbox) {
    this.dropbox = dropbox;
}

public String getEvernote() {
    return evernote;
}

public void setEvernote(String evernote) {
    this.evernote = evernote;
}

public String getGoogleDocs() {
    return googleDocs;
}

public void setGoogleDocs(String googleDocs) {
    this.googleDocs = googleDocs;
}

public String getSubject() {
    return subject;
}

public void setSubject(String subject) {
    this.subject = subject;
}

public String getProjectName() {
    return projectName;
}

public void setProjectName(String projectName) {
    this.projectName = projectName;
}

public String getEndDate() {
    return endDate;
}

public void setEndDate(String endDate) {
    this.endDate = endDate;
}

public String getmembers() {
    return members;
}

public void setmembers(String members) {
    this.members = members;
}

public String getUserid() {
    return userid;
}

public void setUserid(String userid) {
    this.userid = userid;
}
}
1
What is wrong with your code? - Alex Mamo
I'm not getting down the right group for that user - Oscar
So to understand better, you need to get all groups a user is in? - Alex Mamo
Exactly. Do you have any suggestions? - Oscar
Yes I have but not without changing your database structure. Are you ok with that? - Alex Mamo

1 Answers

0
votes

Using the actual database structure, you cannot get all the groups a user is in, unless you make some changes to your database. In your case, you should consider augmenting your database structure to allow a reverse lookup by creating another node, named groups under each user object, in which you should add all the group ids from which the user is appart of. So your database structure should look like this:

Firebase-root
   |
   --- users
         |
         --- userId
               |
               --- groups
                     |
                     --- groupIdOne: true
                     |
                     --- groupIdTwo: true

To get all the groups a user is appart, just attach a listener on the groups node and there are all the groups that you need.

But you'll be thinking, why to do this? Why to duplicate data? Well, there is no problem with duplicating data, when it comes to Firebase. This is a quite common practice, which is named denormalization and for that, I recommend you see this video, Denormalization is normal with the Firebase Database.

When you are duplicating data, there is one thing that need to keep in mind. In the same way you are adding data, you need to maintain it. With other words, if you want to update/detele an item, you need to do it in every place that it exists.