我使用Firebase值創建了一些簡單的計算系統。但是,如果我評價它,計算和值是正確的,但它不會更新標籤。標籤上的Firebase值未更新
這就是我得到的值,做計算:
let ref = FIRDatabase.database().reference().child("Snuses").queryOrdered(byChild: "Brand")
.queryEqual(toValue: brandName)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists(){
let enumerator = snapshot.children
while let thisProduct = enumerator.nextObject() as? FIRDataSnapshot {
// Chances are you'd have to create a dictionary
let thisProductDict = thisProduct.value as! [String:AnyObject]
let rating = thisProductDict["rating"] as! Double
let ratersCount = thisProductDict["ratersCount"] as! Double
let ratingToShow: String = String((ratersCount == 0) ? 0 : rating/ratersCount)
let productObject = Product(
rating: rating,
ratersCount: ratersCount,
ratingToShow: ratingToShow)
self.products.append(productObject)
}
self.tableView.reloadData()
而在cellForRowAtIndexPath
我試圖說明標籤上的ratingToShow:
cell.ratingLabel.text = products[indexPath.row].ratingToShow
我添加值是這樣的方式:
let ratingToShow: String = String((products[indexPath.row].ratersCount == 0) ? 0 : products[indexPath.row].rating/products[indexPath.row].ratersCount)
cell.likeLabel.text = ratingToShow
self.databaseRef.child("productRatings").child(self.currentUser.generalDetails.uid).child(self.products[indexPath.row].snusProductTitle).child(self.currentUser.generalDetails.uid).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.value as? Bool == true{
self.databaseRef.child("productRatings").child(self.currentUser.generalDetails.uid).child(self.products[indexPath.row].snusProductTitle).child("rating").observeSingleEvent(of: .value, with: { (snapshot) in
let currentUserRate = snapshot.value
cell.ratingView.rating = currentUserRate as! Double
})
}else{
cell.ratingView.rating = 0.0
}
cell.ratingView.didFinishTouchingCosmos = { rating in
if snapshot.value as? Bool == true{
self.databaseRef.child("productRatings").child(self.currentUser.generalDetails.uid).child(self.products[indexPath.row].snusProductTitle).child("rating").observeSingleEvent(of: .value, with: { (snapshot) in
let currentUserRate = snapshot.value as? Double
self.databaseRef.child("Snuses").child(self.products[indexPath.row].snusProductTitle).child("rating").runTransactionBlock({
(currentData:FIRMutableData!) in
var value = currentData.value as? Double
if (value == nil) {
value = 0.0
}
currentData.value = value! - currentUserRate!
cell.update(rating)
return FIRTransactionResult.success(withValue: currentData)
})
self.databaseRef.child("Snuses").child(self.products[indexPath.row].snusProductTitle).child("rating").runTransactionBlock({
(currentData:FIRMutableData!) in
var value = currentData.value as? Double
if (value == nil) {
value = 0.0
}
currentData.value = value! + rating
cell.update(rating)
return FIRTransactionResult.success(withValue: currentData)
})
self.databaseRef.child("productRatings").child(self.currentUser.generalDetails.uid).child(self.products[indexPath.row].snusProductTitle).runTransactionBlock({
(currentData:FIRMutableData!) in
var value = currentData.value as? Bool
if (value == nil) {
value = true
}
currentData.value = [self.currentUser.generalDetails.uid:true]
self.databaseRef.child("productRatings").child(self.currentUser.generalDetails.uid).child(self.products[indexPath.row].snusProductTitle).updateChildValues(["rating": rating])
return FIRTransactionResult.success(withValue: currentData)
})
})
}else{
self.databaseRef.child("Snuses").child(self.products[indexPath.row].snusProductTitle).child("ratersCount").runTransactionBlock({
(currentData:FIRMutableData!) in
var value = currentData.value as? Double
if (value == nil) {
value = 0.0
}
currentData.value = value! + 1
return FIRTransactionResult.success(withValue: currentData)
})
self.databaseRef.child("Snuses").child(self.products[indexPath.row].snusProductTitle).child("rating").runTransactionBlock({
(currentData:FIRMutableData!) in
var value = currentData.value as? Double
if (value == nil) {
value = 0.0
}
currentData.value = value! + rating
cell.update(rating)
return FIRTransactionResult.success(withValue: currentData)
})
self.databaseRef.child("productRatings").child(self.currentUser.generalDetails.uid).child(self.products[indexPath.row].snusProductTitle).runTransactionBlock({
(currentData:FIRMutableData!) in
var value = currentData.value as? Bool
if (value == nil) {
value = true
}
currentData.value = [self.currentUser.generalDetails.uid:true]
self.databaseRef.child("productRatings").child(self.currentUser.generalDetails.uid).child(self.products[indexPath.row].snusProductTitle).updateChildValues(["rating": rating])
return FIRTransactionResult.success(withValue: currentData)
})
}
}
})
我應該做什麼改變?
有問題中沒有足夠的信息來理解問題,因爲在填充單元格文本的代碼中問題似乎更多。但是,如果您正在使用產品對象,則會更像cell.likeLabel.text = productObject.ratingToShow – Jay
我有產品結構也是。 –
那麼,你的問題中的代碼在概念上是可以的,但是,你顯然缺少productName,snusNicotine等來填充ProductObject,因此代碼不能按原樣工作。我認爲我們需要看看填充單元的代碼。此外,您可能希望在這裏和那裏拋出一個打印語句,以確保產品數組中的productObjects不會被更改或省略。 – Jay