2016-11-03 18 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

実行されますので、runTransactionBlock年代がローカルキャッシュにロードされますがあります問題がセルテキストを埋めるコードのほうにあるように見えるため、問題を理解するための十分な情報が不足しています。しかし、製品オブジェクトを使って作業しているのであれば、cell.likeLabel.text = productObject.ratingToShowのようになります。 – Jay

+0

私はProduct structureも持っています。 –

+0

まあ、あなたの質問のコードは概念的には大丈夫ですが、明らかにproductName、snusNicotineなどが見つからないので、ProductObjectを実装してコードがそのまま動作することはありません。私はセルに値を設定するコードを見てみる必要があると思います。また、製品の配列内のproductObjectsが改ざんされたり省略されたりしないように、ここに印字ステートメントをスローすることもできます。 – Jay

答えて

0

が簡単にユーザーインターフェイスで増分の値を操作するには、一般的な、最も基本的なアプローチは、このような通りです: - あなたのFirebaseデータベース内の投稿

  • 更新:使用runTransactionBlock

  • 次に、現在のデータソースを、Product構造体の検索して保存したデータと同じ値に更新します。

    例えばのために

: - ユーザーがそれを好むか、またはそれを嫌う、あなたのバックエンド(Firebase)で、あなたの更新のそれぞれの行動の価値、そしてまたproducts[indexPath.row].ratingToShowで、その後、self.tableView.reloadDataを呼びたいボタンをクリックすると。この方法では、ユーザーがあなたの帯域幅を節約している投稿を好き嫌いをするたびに、あなたのデータベースから現在の好き嫌いを取り出す必要はありません。

あなたのようなボタンは、それがその後、ちょうどyourTableViewControllerにコントローラの参照を作成し、データソースに変更TableViewCellクラスで@IBActionのしている場合: - ちょうど、indexPathForCellを取得するための

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 
    } 
    } 

を値を初期化する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 
     } 

とポストを好きか嫌いながら、ネットワーク接続を失うユーザーを心配していないユーザーがネットワーク接続を取り戻したら、これらのコマンドは

+0

問題は、好きではない、評価です。私はそれを計算しなければならない。だから私は美容的に値を追加することはできません。好きなシステムは簡単です。 –

+0

あなたは評価が平均ですか?あなたのユーザーレートがratersCountで一度加算されると、アルゴリズムをまとめて、ユーザーを(データソースとバックエンドで同時に)レートを決めるレートを+1して評価し、UIで計算して提示すれば、あなたのバックエンドに電話して、変更が瞬時に反映され、その唯一の数学、好みと評価は潜在的に同じであるか、または数学的操作をほとんど受けません。 – Dravidian

+0

私はそのためのアルゴリズムを持っています。あなたが私の与えられたコードを見るなら、あなたはそれを見るべきです。 –

関連する問題