总而言之,我正在Swift中制作一个餐厅评分应用程序,它可以让用户发表评论并给餐厅评分(从1到5星级评分)。该应用程序不需要注册屏幕或登录名,用户可以直接对餐厅进行投票和评论。我有两个问题。
我可以成功地在某个餐厅获得特定用户的评分,并将其存储在Firebase数据库中,但事实是,我不能阻止同一用户多次投票。我只希望每个用户只投票一次,他们可以发表自己想发表的评论,但他们的星级应仅限于一个。我认为这与他们的uid有关,但我无法解决问题。另外,如何显示餐厅的平均评分?我的意思是我应该如何保存它并在何处显示它?
到目前为止,这是我的代码。我将不胜感激!提前非常感谢您!
@IBAction func button(_ sender: Any) {
let ref = Database.database().reference()
let commentsReference = ref.child(locationName).child("comments")
let newCommentID = commentsReference.childByAutoId().key
let newCommentReference = commentsReference.child(newCommentID)
Auth.auth().signInAnonymously { (authResult, error) in
if let error = error {
print("Sign in failed:", error.localizedDescription)
} else {
let user = authResult?.user
let isAnonymous = user?.isAnonymous // true
let uid = user?.uid
// print ("Signed in with uid:", authResult!.uid)
var currentUserID = uid
if let currentUser = Auth.auth().currentUser {
currentUserID = currentUser.uid
}
newCommentReference.setValue(["uid": currentUserID as Any, "userRating":self.ratingStackView.starsRating]) { (error, ref) in
//ratingStakView.starsRating successfully returns the number of stars the user has chosen from my other viewController
if error != nil {
print(error!.localizedDescription)
return
}
}
}
}
}