how to limit star-count on Firebase?

泪湿孤枕 提交于 2020-01-03 05:07:13

问题


I am writing an anonymous social media app with a universal feed. I want to limit the likes on a post to one per user, but I don't know how to limit the likes of each user. This is my current code:

    class story
var text = ""
var numberoflikes = 0
var user = Auth.auth().currentuser!

iboulet weak var : likebutton : uibutton
iboulet weak var: storylabel : uilabel

var story: story! {
didSet {
storylabel.text = story.text
likebutton.setTitle(" /("story.numberoflikes)", for: []}}
}

ibaction func likedidtouch: (_ sender: AnyObject) {
story.like()
llikebutton.settitle(" \(story.numberoflikes)", for: []}}
}}

extension story {
func like() {
if like() set user = false
else set user = true
numberoflikes += 1 
ref.child("numberoflikes").setValue(numberoflikes
}
}

What do I need to change in order to limit the likes of each user to one?

my database model, i have:
"stories"> "text", "numberoflikes"

I would really appreciate any help you can give me on this


回答1:


I've recently created a similar platform. What I did was storing the user votes in my database as well.

Within each user's node they have their vote values for every single post. So lets say you want to have buttons with highlights like below. You will have to fetch the postVotes node for that user from the tree, loop over the postVotes object and assign highlights depending on each post's vote value.

Lastly you should compare user's old vote that you keep on the firebase with his new vote.

let incrementBy
let userVote

if (currentVote === newVote * -1) {
  incrementBy = newVote * 2
  userVote = newVote
}
else if(currentVote === 0) {
  incrementBy = newVote
  userVote= newVote
}
else {
  incrementBy = newVote * -1
  userVote= 0
}

// update your totalvote value on your firebase by incrementBy
// swap your firebase uservote value on that post with userVote



回答2:


There's many way to approach this but here are two. Either way, start with this Firebase structure.

posts
  some_post_id
     post: "I like pizza"
     votes
      uid_0: true
      uid_2: true
      uid_5: true

1) See if the voting user has already voted by checking to see if their Anonymous User Id (UID) already exists

In essence, when a user votes for a post, capture their uid and write it to the votes node with a true value if it doesn't already exist.

Before writing, check to see if they already voted

let thisUid = Auth.auth().currentUser?.uid
let postsRef = self.ref.child("posts")
let refToCheck = postsRef.child("some_post_id").child("votes").child(thisUid!)

refToCheck.observeSingleEvent(of: .value, with: { snapshot in
    if snapshot.exists() {
        print("you already voted for this post")
    } else {
        print("no uid found, adding a vote")
        refToCheck.setValue(true)
    }
})

note this needs error checking but is functional.

2) Use Firebase Rules to allow a write only the uid (key) doesn't already exist within the votes node. Essentially it will deny the write if the uid already exists). I like #1 better but that is an alternative.

If you are not familiar with rules there are a lot of options so take a look a the Firebase Rules Guide first.



来源:https://stackoverflow.com/questions/51811974/how-to-limit-star-count-on-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!