是否可以将喜欢(从用户 A 到用户 B)限制为每天或会话一个?



我现在拥有的:一个显示用户收到的页面。如果有人一直点击"赞"按钮,则每个"赞"都会显示在此页面上。这太过分了。我想这样做,即使有人连续点击喜欢按钮,也只会显示其中一个喜欢。换句话说,我需要的是一种将显示(制造(的喜欢限制为每天或会话 1 个的方法。

我一直在来回尝试决定是否在按下喜欢或显示喜欢时进行限制(显然在不同的页面上为不同的用户(。

这是看到他/她自己的所有喜欢的用户页面的一些代码。

func printPersonInfo(uid: String) {
print(uid)
let usersRef = Database.database().reference().child("people")
let thisUser = usersRef.child(uid)
thisUser.observeSingleEvent(of: .value, with: { snapshot in
let photoPosts = snapshot.childSnapshot(forPath: "PhotoPosts").value as? String ?? "No PhotoPosts"
let education = snapshot.childSnapshot(forPath: "Education").value as? String ?? "No Education"
let whatIamConsideringBuying = snapshot.childSnapshot(forPath: "WhatIamConsideringBuying").value as? String ?? "No WhatIamConsideringBuying"
print(photoPosts, education, whatIamConsideringBuying)
let p = Usery(education: education, whatIamConsideringBuying: whatIamConsideringBuying, photoPosts: photoPosts)
self.person.insert(p, at: 0)
self.table.reloadData()

这里有一些更多的下游

public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell
let immy = cell.viewWithTag(2) as! UIImageView
let person1: Usery = person[indexPath.row]

cell.lblName1.text = person1.education
cell.lblgenre1.text = person1.whatIamConsideringBuying
if let photoPosts = person1.photoPosts {
let url = URL(string: photoPosts)
immy.sd_setImage(with: url)
}
return cell
}

这是数据库结构:

"peeps" : {
"5VbL3Adj7teM2KNJkF4GIexBJhE2" : {
"Education" : "Yale",
"PhotoPosts" :     "https://firebasestorage.googleapis.com/v0/b/daylike-2f938.appspot.com/o/images%2FPhotoPosts?alt=media&token=42d76567-ac42-4728-9914-1d7c2fa4d5e6",
"WhatIamConsideringBuying" : "Twitter: Carla9",
"caption1" : {
"caption1" : 1570097725719,
"keyToPost" : "-LqG6V0RVEyFNt9Zu9CE"
},
"likes" : 565,
"peopleWhoLike" : {
"-LmLjHwwGj1kt5qLM20X" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",
"-LmLtlp5Sm900SV8xP4i" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",

进行点赞的页面包含按下点赞时的按下功能。

(回答后更新(

let ref = Database.database().reference()
let keyToPost = ref.child("likes").childByAutoId().key
ref.child("people").child(self.postID).observeSingleEvent(of: .value, with:  {(snapshot) in
if let people = snapshot.value as? [String: AnyObject] {
let updateLikes: [String: Any] = ["peopleWhoLike/(keyToPost)" : Auth.auth().currentUser!.uid]
ref.child("people").child(self.postID).updateChildValues(updateLikes, withCompletionBlock: { (error, reff) in

if error == nil {
ref.child("people").child(self.postID).observeSingleEvent(of: .value, with: { (snap) in
if let properties = snap.value as?[String: AnyObject]{
if let likes = properties["peopleWhoLike"] as? [String : AnyObject] {
let count = likes.count
let update = ["likes" : count]
ref.child("people").child(self.postID).updateChildValues(update)

附加代码

let uid = Auth.auth().currentUser!.uid
print("H", uid)
let thisUserRef = Database.database().reference().child("people").child(uid)
let myPeopleRef = thisUserRef.child("peopleWhoLike")
myPeopleRef.observeSingleEvent(of: .value, with: { snapshot in
let peopleArray = snapshot.children.allObjects as! [DataSnapshot]
for person in peopleArray {
let personUid = person.value as! String
self.printPersonInfo(uid: personUid)
}
})

如果您想在Firebase(以及许多其他NoSQL数据库(上设置这种类型的限制,请考虑将其建模到您的数据中。

在当前模型中,每次新投票都使用推送 ID。

"peopleWhoLike" : {
"-LmLjHwwGj1kt5qLM20X" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",
"-LmLtlp5Sm900SV8xP4i" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2"
}

由于每次您调用push/childByAutoId()时都会保证生成一个唯一的新位置,因此用户可以根据需要随时生成赞成票。


如果您只希望每个用户能够投票一次,则可以像这样存储数据:

"peopleWhoLike" : {
"NMNQYJ5z64fATK2MMs7m0ggHl0k2": true,
"another-Uid-ThatIs-Different": true
}

现在,如果同一个人再次尝试投票,他们只是再次将相同的数据设置为 true,因此没有任何变化。

上面的true是没有意义的(你不能在没有值的情况下存储密钥(,但你也可以用它来跟踪用户点击喜欢按钮的频率:

"peopleWhoLike" : {
"NMNQYJ5z64fATK2MMs7m0ggHl0k2": 3,
"another-Uid-ThatIs-Different": 2
}

通过这种方式,您可以确定有多少人喜欢该帖子(通过计算节点数量(,也可以确定他们喜欢该帖子的次数(通过对值求和(。


如果你想允许用户每个时间间隔点赞一次,你也可以将其建模到你的数据模型中:

"peopleWhoLike" : {
"20191010": {
"NMNQYJ5z64fATK2MMs7m0ggHl0k2": true
},
"20191010": {
"NMNQYJ5z64fATK2MMs7m0ggHl0k2": true,
"another-Uid-ThatIs-Different": true
}
}

要编写这种类型的结构,您可以使用如下内容:

ref.child("people")
.child(self.postID)
.child("peopleWhoLike")
.child("20191012")
.child(Auth.auth().currentUser!.uid)
.setValue(true)

为此,请跟踪用户何时喜欢该帖子。如果用户今天不喜欢该帖子,那么他们可以继续。如果没有,请显示一条消息,告诉他们可能不喜欢该帖子。

在伪代码中:

if (user has liked post && the like is today) {
display message
} else {
allow the like to go through
}

最新更新