有效地确定用户是否喜欢在Firebase中发帖

我有一个应用程序,用户可以喜欢的post,我想确定当前用户以前是否喜欢一个有效的方式发布。 我的数据目前看起来像这样:

发布数据

我也为每个用户存储喜欢

用户喜欢

在我目前的查询中,我这样做:

if let people = post["peopleWhoLike"] as? [String: AnyObject] { if people[(Auth.auth().currentUser?.uid)!] != nil { posst.userLiked = true } } 

不过,我相信这需要我下载所有的post里的效率不是很高,所以我试了一下:

  if (post["peopleWhoLike\(Auth.auth().currentUser!.uid)"] as? [String: AnyObject]) != nil { posst.userLiked = true } 

第二种方法似乎没有正常工作。 有一个更好的方法吗?

这是我最初的查询:

 pagingReference.child("posts").queryLimited(toLast: 5).observeSingleEvent(of: .value, with: { snap in for child in snap.children { let child = child as? DataSnapshot if let post = child?.value as? [String: AnyObject] { let posst = Post() if let author = post["author"] as? String, let pathToImage = post["pathToImage"] as? String, let postID = post["postID"] as? String, let postDescription = post["postDescription"] as? String, let timestamp = post["timestamp"] as? Double, let category = post["category"] as? String, let table = post["group"] as? String, let userID = post["userID"] as? String, let numberOfComments = post["numberOfComments"] as? Int, let region = post["region"] as? String, let numLikes = post["likes"] as? Int { 

解决了它:

在tableView中,我只是直接查询喜欢的值,然后确定显示哪个button。

 static func userLiked(postID: String, cell: BetterPostCell, indexPath: IndexPath) { // need to cache these results so we don't query more than once if newsfeedPosts[indexPath.row].userLiked == false { if let uid = Auth.auth().currentUser?.uid { likeRef.child("userActivity").child(uid).child("likes").child(postID).queryOrderedByKey().observeSingleEvent(of: .value, with: { snap in if snap.exists() { newsfeedPosts[indexPath.row].userLiked = true cell.helpfulButton.isHidden = true cell.notHelpfulButton.isHidden = false } }) likeRef.removeAllObservers() } } } 

在我的TableView中调用:

 DatabaseFunctions.userLiked(postID: newsfeedPosts[indexPath.row].postID, cell: cell, indexPath: indexPath)