I am making this app where users can give feedback on other users resumes. I have the feedback stored in a firebase realtime database. In my FeedbackViewController, I have a tableview and need to access the totalFeedbackNum on my database in order to populate my tableview, however I am getting an error. The commented code below is what the code should be but it's throwing the error: Unexpected non-void return value in void function.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// var ref: DatabaseReference!
// ref = Database.database().reference()
// let uid = Auth.auth().currentUser?.uid
// ref.child("usernames/\(uid!)/profile/feedback/totalFeedbackNum").observeSingleEvent(of: .value, with: { (snapshot) in
// let value = snapshot.value as? NSDictionary
// let totalFeedbackNum = value?["totalFeedbackNum"] as? Int ?? 0
// return totalFeedbackNum //this throws "Unexpected non-void return value in void function"
// }) { (error) in
// print(error.localizedDescription)
// }
let totalFeedbackNum = 3
return totalFeedbackNum
}
I think I should be using a completion handler but I don't know how. I'm working with Swift 5.
Edit: I followed the first link in the comments and got this but still it's not working:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let uid = Auth.auth().currentUser?.uid
var ref: DatabaseReference!
ref = Database.database().reference()
var totalFeedbackNum = 1
print("Starting observing")
ref.child("usernames/\(uid!)/profile/feedback/totalFeedbackNum").observe(.value, with: { (snapshot: DataSnapshot!) in
print("Got snapshot")
let value = snapshot.value as? NSDictionary
totalFeedbackNum = value?["totalFeedbackNum"] as? Int ?? 0
})
print("Returning totalFeedbackNum: \(totalFeedbackNum)");
return totalFeedbackNum
}
This code prints: Starting observing Returning totalFeedbackNum: 1 Starting observing Returning totalFeedbackNum: 1 Got snapshot Got snapshot