1

I have been trying (without success) to retrieve a list of users in a group from a Firebase database; and I was wondering if its possible based on my data structure (below), or is the problem my swift code (also below)

This is my Firebase structure:

Users
---- <UserID>
---------- Username
---------- Email
---------- etc...
Groups
---- <GroupID>
---------- GroupName
---------- CreationDate
---------- GroupAdmin
---------- etc...
UsersInGroups
------- UserID
---------- GroupID : true   <---- User is in Group

With the above data structure is it possible for me to retrieve the list of all users in the a particular group?

Currently my swift code is as follows:

ref = Database.database()reference(withPath: "UsersInGroups")
handle = ref.queryOrdered(byChild: <userID>).queryEqual(toValue: true).observe(.value, ....

As you can imagine, it is not pulling the userID where the groupID = true!?

Lastly, I was wondering if this is possible: I would like to get a list of all the GroupEntries a User has done.

the Firebase structure is as follows:

GroupEntry
-------- <GroupID>
--------------- <entryID> : <userID>

the is a dynamic and unique string (ex 3:8) and the userID is the user that created the entry.

The swift code is below:

ref = Database.database()reference(withPath: "GroupEntry")
handle = ref.child(<groupID>).queryEqual(toValue: <userID>).observe(.value, ...

Can anyone offer any assistance!?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Learn2Code
  • 1,974
  • 5
  • 24
  • 46
  • 1
    Did you try using the `observe` method on your query? – shim Nov 24 '17 at 18:41
  • 1
    Yes, I just updated the question with the `observe` portion – Learn2Code Nov 24 '17 at 18:43
  • Does it work without the query? – shim Nov 24 '17 at 18:45
  • This is a categorization problem. The best solution is to add an additional top-level list of `GroupUsers` (the inverse of your `UsersInGroups`. See http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Nov 24 '17 at 18:55
  • @FrankvanPuffelen thats what I was figuring... I just didnt want to face the facts.... :( How about the second portion, do I need to do the same thing!? I hope not!? – Learn2Code Nov 24 '17 at 19:14

1 Answers1

2

You can represent the relationships between the groups and users using the following locations:

  • group-users/$groupId/$uid
  • user-groups/$uid/$groupId

Storing both inversions of the relationship will give you more querying abilities. To retrieve the users in a group, you observe the children at group-users/$groupId – on the other hand, you can get the groups a user is in by observing the children at user-groups/$userId.

Group entries can be represented with two more locations:

  • group-entries/$groupId/$entryId
  • user-entries/$userId/$entryId

You could get all the entries a user has made by observing user-entries/$userId; you can query further by ordering by the child that contains the entry's $groupId.

The challenge with all of these locations is maintaining them – ensuring that data is kept consistent throughout. This can be in a somewhat manual way using one of the Firebase client SDKs, however you could consider using Cloud Functions to create triggers that update the relevant locations in the database.

Callam
  • 11,409
  • 2
  • 34
  • 32