Background
I am creating an app where users can submit a post at any time. One of the main feature of the app is that users in a group can start a session to vote for the best posts, within a period of time (e.g. all post from the last 10 days).
The app then presents the posts in a table view. Users can change the type of posts displayed using a segmented control. Once all users have voted, then the next view controller will display the result of the vote - showing only the top 5 posts for each category ("well", "notWell", "confused")
This is how I have structured it in Firebase Database.
- Each user must belong to a group - e.g. "Balance Banana".
- Every post must belong to one of three category - e.g. "well", "notWell", "confused".
- Each post must have a date.
Question
How should I develop the feature to "display top 5 posts for each category"?
I need to retrieve 5 post with the highest vote count for each post type where date is > x.
I have looked at the several questions on SO relating to querying multiple where clauses with Firebase Database and I think the most suitable approach is to use the QueryOrderedBy to filter the date, then code the logic to extract the top 5 posts within each category. If this is a suitable approach, how should I go about iterating through each snapshot.children and create a dictionary of top 5 posts for each category?
SO Posts related to multiple where clause in Firebase Database.
Query based on multiple where clauses in firebase has an excellent answer by Frank van Puffelen relating to this topic.
Current code
func loadData() {
self.ref.child("session").child((self.currentUser?.team)!).queryOrdered(byChild: "date").queryStarting(atValue: postStartDate).observe(.value, with: {
snapshot in
var wellItems: [SessionPost] = []
var notWellItems: [SessionPost] = []
var confusedItems: [SessionPost] = []
for item in snapshot.children {
let postItem = SessionPost(snapshot: item as! DataSnapshot)
if let postType = postItem.type {
switch postType {
case "well":
wellItems.append(postItem)
case "notWell":
notWellItems.append(postItem)
case "confused":
confusedItems.append(postItem)
default:
break
}
}
}
self.topWellPostItems = wellItems
self.topNotWellPostItems = notWellItems
self.topConfusedPostItems = confusedItems
self.tableView.reloadData()
})