2016-10-20 7 views
0

私はこれのためのよりよい解決策を知らなかったので、私はシステムのように簡単にしました。しかし、問題は、ユーザーが何かを初めて好きなのであれば、trueの代わりにfalseに値を設定し、私のコードでバグを見つけることができないということです。私はボタンをタップするだけで動作するようです。値がfalseの場合、このようFirebaseのようなシステムが正しく動作しない

私は、cellForRowAtIndexPath内部の他に嫌い「のような」ボタンのラベルを作るチェック:

databaseRef.child("postLikes").child(currentUser.generalDetails.uid).child(postsArray[indexPath.row].key).observeSingleEvent(of: .value, with: { (snapshot) in 

      if snapshot.value as? Bool == true{ 
       print("has liked") 
       cell.likeButton.setTitle("Dislike", for: .normal) 
      }else{ 

       print("hasn't liked") 
       cell.likeButton.setTitle("Like", for: .normal) 
      } 
     }) 

そして、それは、ボタンを押したときに、私が呼び出す関数です。

func like(sender: UIButton){ 
     let section = 0 
     let row = sender.tag 
     let indexPath = IndexPath(row: row, section: section) 
     let cell: FeedTableViewCell = tableView.dequeueReusableCell(withIdentifier: "feedCell", for: indexPath) as! FeedTableViewCell 
     if cell.likeButton.titleLabel?.text == "Like"{ 
     self.databaseRef.child("postLikes").child(currentUser.generalDetails.uid).child(postsArray[indexPath.row].key).runTransactionBlock({ 
      (currentData:FIRMutableData!) in 
      var value = currentData.value as? Bool 

      if (value == nil) { 
       value = false 
      } 
      currentData.value = true 
      return FIRTransactionResult.success(withValue: currentData) 

     }) 

     self.databaseRef.child("posts").child(postsArray[indexPath.row].key).child("likes").runTransactionBlock({ 
      (currentData:FIRMutableData!) in 
      var value = currentData.value as? Int 

      if (value == nil) { 
       value = 0 
      } 
      currentData.value = value! + 1 
      return FIRTransactionResult.success(withValue: currentData) 

     }) 

     }else{ 
      self.databaseRef.child("posts").child(postsArray[indexPath.row].key).child("likes").runTransactionBlock({ 
       (currentData:FIRMutableData!) in 
       var value = currentData.value as? Int 

       if (value == nil) { 
        value = 0 
       } 
       currentData.value = value! - 1 
       cell.likeButton.titleLabel?.text = "Like" 
       return FIRTransactionResult.success(withValue: currentData) 
     }) 
      self.databaseRef.child("postLikes").child(currentUser.generalDetails.uid).child(postsArray[indexPath.row].key).runTransactionBlock({ 
       (currentData:FIRMutableData!) in 
       var value = currentData.value as? Bool 

       if (value == nil) { 
        value = false 
       } 
       currentData.value = false 
       return FIRTransactionResult.success(withValue: currentData) 
     }) 
     } 
    } 

を何問題を引き起こすはずですか?

答えて

0

問題は、あなたがこの行にこの

if snapshot.value as? Bool != true{ 
      print("has liked") 
      cell.likeButton.setTitle("Dislike", for: .normal) 
} else { ... } 

if snapshot.value as? Bool == true {} 

を変更した場合よりも、持続しかし、問題は、それも動作していない

if cell.likeButton.titleLabel?.text == "Like"{ 
    self.databaseRef.child("postLikes").child(currentUser.generalDetails.uid).child(postsArray[indexPath.row].key).runTransactionBlock({ 
     (currentData:FIRMutableData!) in 
     var value = currentData.value as? Bool 

     if (value == nil) { 
      value = false 
     } 




     currentData.value = true // <-- should be false 

     return FIRTransactionResult.success(withValue: currentData) 

    }) 
+0

この部分に思えるかどうかをチェックしてみてください'' = '' '' ''ではないので、これは期待されています:Dそれは同じに動作します。ダブルクリックすると動作します。あなたの答えは論理が間違っています。彼が好きでない場合は、タイトルは "Like"でなければならないelse "Dislike"反対でない –

+0

あなたのバックエンドのロジックが真であるかどうかはチェック用でした – Achron

+0

あなたのロジックのexplane – Achron

関連する問題