Swift how to add 5 star ratings for users to vote using Firebase

Swift how to add 5 star ratings for users to vote using Firebase



I'm creating a restaurant rating app in Swift that allows its users to rate restaurants on a 5-star rating scale (just like Zomato and Yelp) .
I will be using Firebase to store each user's rating for a specific restaurant and then take the average of user ratings and display it in the app. There will be a star rating corresponding to each tableview cell (every tableview cell corresponds to a restaurant) .



However I'm stuck deeply on the project. I can't figure out how to add the user's rating to the database. My app won't require a user sign-up or login page, users can directly rate restaurants. This being said, also, how can I make sure that users can vote only once for a single restaurant? I'd appreciate any help! Thank you in advance.



Here is my code so far:


var starsRating = 0
var starsEmptyPicName = "star" // can change to empty star picture name
var starsFilledPicName = "starfill" // can change to filled star picture name
override func draw(_ rect: CGRect)
let starButtons = self.subviews.filter$0 is UIButton
var starTag = 1
for button in starButtons
if let button = button as? UIButton
button.setImage(UIImage(named: starsEmptyPicName), for: .normal)
button.addTarget(self, action: #selector(self.pressed(sender:)), for: .touchUpInside)
button.tag = starTag
starTag = starTag + 1


setStarsRating(rating:starsRating)


func setStarsRating(rating:Int)
self.starsRating = rating
let stackSubViews = self.subviews.filter$0 is UIButton
for subView in stackSubViews
if let button = subView as? UIButton
if button.tag > starsRating
button.setImage(UIImage(named: starsEmptyPicName), for: .normal)
else
button.setImage(UIImage(named: starsFilledPicName), for: .normal)





@objc func pressed(sender: UIButton)
setStarsRating(rating: sender.tag)


}



And here are my tableview functions:


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return identifiers.count //identifiers is my big restaurants array




func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "CellThree")
cell.textLabel!.text = identifiers[indexPath.row]
return cell





Here's the issue My app won't require a user sign-up or login page. Without having a user log in or some kind of authentication, there is no way to track it. The app needs to know who is voting and store that data in an /already_voted/ node. Then either see if that user already voted in code, or leverage Firebase Rules to deny a write if they have already voted. You can use Anonymous Authentication to get some traceability but that's not really a foolproof method. It's best to create users and store their uid's.
– Jay
Aug 26 at 13:59




1 Answer
1



I would use CoreData to save if user already vote.



When the user rate one of the restaurant, just create a new record in core data with the restaurant's name or ID.



Before every vote I should check if data is existing or not. If not, user can vote.





While CoreData is very powerful, in this use case it's kind of like using a cannon to kill a fly. Why add a huge amount of overhead to store a single vote that could easily just be stored in a plist or even in userDefaults as a couple of bytes (keep reading). More importantly - and here's the real issue - the vote would only be stored on that device. If the user has multiple devices (like many of us do), they could vote over and over - which, if you think about it, they could post their login online and anyone that saw it could log into his account and vote.
– Jay
Aug 26 at 14:01





@Jay Your right, it was my idea to do it without user authentication, but the best solution would be some kind of registration system.
– Dris
Aug 26 at 16:12






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)