What I have right now: A page that displays all like that a user has received. If someone keeps hitting the like button, each like will be displayed on this page. That is too much. I want to make it so that even if someone hits the like button continuously, only one of those likes will be displayed. In other words what I need is a way to limit the likes displayed (of made) to 1 per day or session.
I have been going back and forth trying to decide whether to make the limit when the like is pressed or when the like is displayed (on different pages for different users obviously).
Here is some code for the page of the user who sees all the likes of him/herself.
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()
Here's some more downstream
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
}
This is the database structure:
"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",
The page where the like is made contains a Pressed function for when like is pressed.
(UPDATE after answer)
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)
additional code
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)
}
})