我在使用键 status
更新我的 Firebase 数据库中的值时遇到问题。我只是不知道如何访问它是 autoID 的 child(id)。我的 Swift 3 实体中应该有一个属性吗?还是有 child (autoID)或类似的东西?我是 Firebase 和 Swift 3 的新手。任何人都可以帮忙吗?这是我的代码:
self.ref?.child("requests").queryOrdered(byChild: "Sender").queryEqual(toValue: self.items[indexPath.row].Sender).setValue("2", forKey: "status"))
最佳答案
swift 3 && 火力地堡 3
希望这段代码能帮到你...
// create the reference you want to observe
let myRequestRef = FIRDatabase.database().reference().child("requests").queryOrdered(byChild: "Sender").queryEqual(toValue: self.items[indexPath.row].Sender)
// check the sender value
myRequestRef.observeSingleEvent(of: .value, with: { (snapshot) in
//get the sender key
let senderKey = snapshot.key as String
// create status ref for new status value
let statusRef = FIRDatabase.database().reference().child("requests").child(senderKey)
// create new dict for status value
let newValue = ["Status": 2] as [String: Any]
statusRef.updateChildValues(newValue, withCompletionBlock: { (error, _) in
if error != nil {
print(error?.localizedDescription ?? "Failed to set status value")
}
print("Successfully set status value")
// Update your UI
DispatchQueue.main.async {
// Do anything with your UI
}
})
}) { (error) in
print("Failed to get snapshot", error)
}
关于ios - 更新值 Firebase Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42798791/