Reputation: 573
I am building an "Fishing with Friends" app that lets users create shared scoreboards for fishing. Each user can log scores for his own logbook. I'm currently saving all the scores in one collection called catchLog. For the individual user's own log I just query where the logged catch has his id.
Each user can also invite friends. Each user then has a friends sub-collection with friend ID's.
But now, I'm struggling to think of how to approach showing a shared scoreboard with the different values from catchLog, while filtering for user's friends.
This is the database structure :
Users Collection:
Here is the friends sub-collection (These IDs are the same as those friends uid's) And this person only has one friend. -
And here is the collection where all the catches go to :
I am uncertain of how to approach this problem. I don't think it's possible to call the firestore SDK with a list of values to match in a collection.
I thought I should maybe get a users friend ID's when he opens that view, store them in an array and then make a new database call for every loop through that array but that doesn't feel like a good solution, it would probably take too long.
I also thought of having a Cloud Function that maintains a scoreboard among friends as it's own collection, updating it whenever a new catch is made, but that doesn't work since a new friend can join the group and won't be part of that list.
I have not written any query to try and accomplish this since I'm sure another approach I haven't thought of yet would be able to accomplish this and am thus soliciting advice :) Thanks so much!
Upvotes: 1
Views: 647
Reputation: 4465
I think maintaining a list of feeds is a good idea. A Cloud Function that updates the lists whenever a user becomes a new friend (the function can add all posts by the new friends to their own lists).
For every new or unfriending, the cloud function can add or remove posts from the lists.
For some reason I read this that you had some concept of groups. Rereading it you don’t but will leave this here for possible inspiration. Depending on how many friends you think each person will have, maybe you can consider adding each friend’s UID instead of a group ID:
You can just store alongside the owner (I think your byID field) of each fish caught, which groups that owner belongs to.
So if I’m in groups A , B, and C, all of my future catches will contain A, B, and C and when you’re viewing by group you just need to query for all catches where array contains that group ID.
When a user forms or joins a group, get the ID and add it to all of that user’s catches (you can do all catches in the last X days or limit to last N catches if older data isn’t necessary and you have good fishermen!).
To keep this lost updated when a user leaves a group, you just need to call which items have that user as an owner AND have a group array that contains the groups their in. Pluck that group ID.
You can do this 3 cloud functions by just listening for: new catches, joining a group, leaving a group. Or fewer functions depending on how you store groups!
Upvotes: 2