有效地确定用户是否喜欢在Firebase中发帖 [英] Efficiently determine if user liked post in Firebase
问题描述
我有一个应用程序,用户可以喜欢帖子,我想确定当前用户以前是否以有效的方式喜欢帖子。我的数据目前看起来像这样:
我也存储每个用户喜欢
在我目前的查询中,我我是这样做的:
if let people = post [peopleWhoLike] as? [String:AnyObject] {
if people [(Auth.auth()。currentUser?.uid)!]!= nil {
posst.userLiked = true
}
}
不过,我相信这需要我下载所有的帖子,所以我试过这个:
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
为snap.children中的子元素{
let child = child as?DataSnapshot $ b $如果让post = child?.value as?[String:AnyObject] {
let pos = Post()
如果让author = post [author] as?String,let pathToImage = post [ pathToImage]为?String,让postID = post [postID]为?String,让postDescription = post [postDescription]为?String,让timestamp = post [timestamp]为?Double,让category = post [category] as?String,let table = post [group] as?String,让userID = post [userID]为?String,让numberOfComments = post [numberOfComments]为? region = post [region] as String,让numLikes = post [likes]作为?Int {
解决了它:
I在tableView中,我只是直接查询喜欢的值,然后确定要显示的按钮。
static func userLiked(postID:String,cell:BetterPostCell,indexPath:IndexPath){
//需要缓存这些结果,所以我们不会查询多次
if newsfeedPosts [indexPath.row] .userLiked == false {
如果let uid = Auth.auth()。currentUser?.uid {
)likeRef.child(userActivity)。child(uid).child(likes)。child(postID).queryOrderedByKey()。observeSingleEvent(of:.value,with:{snap in
snap。 exists(){
newsfeedPosts [indexPath.row] .userLiked = true
cell.helpfulButton.isHidden = true
cell.notHelpfulButton.isHidden = false
}
} )
likeRef.removeAllObservers()
}
}
}
在我的TableView中调用:
DatabaseFunctions.userLiked(postID:newsf eedPosts [indexPath.row] .postID,cell:cell,indexPath:indexPath)
I have an app where users can like posts and I want to determine if the current user has previously liked a post in an efficient manner. My data currently looks like this:
I also store the likes for every user
In my current query I am doing this:
if let people = post["peopleWhoLike"] as? [String: AnyObject] {
if people[(Auth.auth().currentUser?.uid)!] != nil {
posst.userLiked = true
}
}
However, I believe this requires me to download all of the post's likes which isn't very efficient, so I tried this:
if (post["peopleWhoLike\(Auth.auth().currentUser!.uid)"] as? [String: AnyObject]) != nil {
posst.userLiked = true
}
The second method doesn't seem to be working correctly. Is there a better way to do this?
Here is my initial query as well:
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 {
Solved it:
In the tableView I just query for the liked value directly and then determine what button to display.
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()
}
}
}
Called in my TableView:
DatabaseFunctions.userLiked(postID: newsfeedPosts[indexPath.row].postID, cell: cell, indexPath: indexPath)
这篇关于有效地确定用户是否喜欢在Firebase中发帖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!