Firebase就像系统无法正常工作

时间:2016-10-20 09:39:57

标签: swift firebase firebase-realtime-database

我做了一些简单的系统,因为我不知道更好的解决方案。但问题是,如果用户第一次喜欢某些内容,则会将值设置为false而不是true,而我似乎无法在代码中找到错误。它似乎只有在我点按两下按钮时才有效。

像这样我检查值是否为假,制作按钮标签"喜欢"其他人不喜欢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)
        })
        }
    }

应该导致什么问题?

1 个答案:

答案 0 :(得分:0)

尝试检查问题是否仍然存在,而不是更改此行

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

用这个

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

但问题似乎在这部分

  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)

    })