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)
})
}
}
を何問題を引き起こすはずですか?
この部分に思えるかどうかをチェックしてみてください'' = '' '' ''ではないので、これは期待されています:Dそれは同じに動作します。ダブルクリックすると動作します。あなたの答えは論理が間違っています。彼が好きでない場合は、タイトルは "Like"でなければならないelse "Dislike"反対でない –
あなたのバックエンドのロジックが真であるかどうかはチェック用でした – Achron
あなたのロジックのexplane – Achron