I have a list of users in an array: let userArray = ["userId1", "userId2"]. I'm trying to pull the list of all comments for a given postId and filter the results to only pull those where the userId is in userArray. Below is my code so far but it doesn't seem to be working. Is there a better way to approach the problem?
//pulls all comments given a postId of "postId1"
let commentsRef = databaseRef.child("comments").queryOrdered(byChild: "postId").queryEqual(toValue: "postId1")
commentsRef.observe(.value, with: { (comments) in
var resultArray = [Comment]()
for comment in comments.children {
let value = child.value as? NSDictionary
let userId = value?["userId"] as? String ?? ""
for contactinfo in userArray {
if(contactinfo = userId) {
let comment = Comment(snapshot: comment as! DataSnapshot)
resultArray.append(comment)
}
}
completion(resultArray)
}
})
})
Firebase:
Comments
-CommentId1
-postId: "postId1"
-userId: "UserId1" //RETRIEVE
-CommentId2
-postId: "postId2"
-userId: "UserId50" //DO NOT RETRIEVE