Firebase中的更新值不会立即在标签上更新

时间:2016-10-04 16:52:26

标签: ios swift firebase firebase-realtime-database

我创建了一个简单的Firebase项目,我可以在Firebase树中更新某些值reputation child。但问题是它立即在Firebase中更新,但在应用程序中没有在标签上更新。这是什么原因?

这是我更新值的代码:

self.databaseRef.child("users").child((loggedInUser?.uid)!).child("reputation").runTransactionBlock({
            (currentData:FIRMutableData!) in
            var value = currentData.value as? Int
            //check to see if the node exists, if not give value of 0.
            if (value == nil) {
                value = 0
            }
            currentData.value = value! + 1
            return FIRTransactionResult.success(withValue: currentData)

        })

我得到的价值如下:

let snapshot = snapshot.value as! [String: AnyObject]

            if let reputation = snapshot["reputation"] {
                self.reputationLabel.text = "\(reputation)"
            } else {
                print("reputation is nil")
            }

1 个答案:

答案 0 :(得分:1)

试试这个: -

    FIRDatabase.database().reference().child("users/\(loggedInUser!.uid)/reputation").runTransactionBlock({ (currentData) -> FIRTransactionResult in
        if let data = currentData.value as? Int{

            currentData.value = data + 1
            return FIRTransactionResult.success(withValue: currentData)
        }else{

            return FIRTransactionResult.success(withValue: currentData)

        }

        }, andCompletionBlock: {(err,completion,updatedData) in

            if completion{

                print(updatedData!.value)
                self.reputationLabel.text = "\((updatedData!.value)!)"
            }else{

                print("Couldnt update")

            }
    })

}