2016-11-03 62 views
0

我使用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) 

         }) 
        } 
       } 
       }) 

我應該做什麼改變?

+0

有問題中沒有足夠的信息來理解問題,因爲在填充單元格文本的代碼中問題似乎更多。但是,如果您正在使用產品對象,則會更像cell.likeLabel.text = productObject.ratingToShow – Jay

+0

我有產品結構也是。 –

+0

那麼,你的問題中的代碼在概念上是可以的,但是,你顯然缺少productName,snusNicotine等來填充ProductObject,因此代碼不能按原樣工作。我認爲我們需要看看填充單元的代碼。此外,您可能希望在這裏和那裏拋出一個打印語句,以確保產品數組中的productObjects不會被更改或省略。 – Jay

回答

0

輕鬆地操縱在用戶界面中的增量值,一般和最基本的方法是這樣的: -

  • 更新您的火力地堡數據庫您的帖子:使用runTransactionBlock

  • 然後在您的Product結構中檢索和存儲的數據中更新您當前的dataSource以獲得相同的值。

對於如: - 一旦用戶點擊了像按鈕,喜歡或不喜歡它,你更新了各自的行動在後端(火力地堡)值,以及在products[indexPath.row].ratingToShow,然後調用self.tableView.reloadData。這樣,每次用戶喜歡或不喜歡某個文章節省帶寬時,您都不必從數據庫中獲取當前的喜歡數。

如果您喜歡按鈕都有它@IBAction在TableViewCell類,然後只是做一個控制器參考yourTableViewController和更改數據源: -

class yourTabelViewCell : UITableViewCell{ 

var tableController : yourTableViewcontroller! 
var indexPathForCell : Int! 


    @IBAction func likeBtnAction(_ sender: UIButton) { 

    //User likes or dislikes the post.... 

     self.tableController.products[self.indexPathForCell]. ratingToShow = //Manipulate the value +1 or -1 
     self.tableController.yourTableView.reloadData() // Given yourTableView is the custom tableView you have implemented in your viewController 
    } 
    } 

爲了得到indexPathForCell,只是初始化您的值cellForRowAt indexPath : _

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "yourTabelViewCell_identifier", for: indexPath) as! yourTabelViewCell 

    cell.likeLabel.text = products[indexPath.row].ratingToShow 
    cell.indexPathForCell = indexPath.row 
    return cell 
     } 

而且不必擔心用戶失去網絡連接,同時喜歡或不喜歡的崗位,runTransactionBlock的在本地緩存加載所以一旦你的用戶恢復網絡連接,這些命令將被執行

+0

問題是,它不喜歡,它是評級。我得計算一下。所以我不能在美觀上增加價值。喜歡的系統很容易。 –

+0

通過評級你平均?只需將算法放在一起,一旦用戶對ratersCount進行評級,就可以將這些算法增加到+1,然後對用戶評分(在數據源和後端同時評分)進行評分,並計算出來並在UI中展示,只需製作一個調用你的後端,這些變化瞬間反映出來,它唯一的數學,喜歡和評分都可能是相同的,或者只是很少的數學操作。 – Dravidian

+0

我有這個算法。如果你看看我的代碼,那麼你應該看到它。 –