Kom*_*ble 2 ios firebase swift firebase-realtime-database
我正在关注此文件.以下是我的更新代码:
func updateDealResultToServer(key:String,dealResult : String)
{
let post = ["dealResul": dealResult]
let childUpdates =
["/komal_xyz/\(key)": post
]
rootRef.updateChildValues(childUpdates)
}
Run Code Online (Sandbox Code Playgroud)
我只想更改值dealResult.每当我尝试运行特定子节点的代码时,1473670100726除了dealResul删除之外的其他值.
要在Firebase实时数据库中的特定节点更新值,请使用: -
你可以使用 runTransactionBlock:
func updateTotalNoOfPost(completionBlock : (() -> Void)){
let prntRef = FIRDatabase.database().reference().child("komal_kyz").child(your_AuroID).child("dealResul")
prntRef.runTransactionBlock({ (resul) -> FIRTransactionResult in
if let dealResul_Initial = resul.value as? Int{
//resul.value = dealResul_Initial + 1
//Or HowSoEver you want to update your dealResul.
return FIRTransactionResult.successWithValue(resul)
}else{
return FIRTransactionResult.successWithValue(resul)
}
}, andCompletionBlock: {(error,completion,snap) in
print(error?.localizedDescription)
print(completion)
print(snap)
if !completion {
print("Couldn't Update the node")
}else{
completionBlock()
}
})
}
Run Code Online (Sandbox Code Playgroud)
在调用此功能时: -
updateTotalNoOfPost{
print("Updated")
}
Run Code Online (Sandbox Code Playgroud)或者只是打电话updateValues
let prntRef = FIRDatabase.database().reference().child("komal_kyz").child(your_AuroID)
prntRef.updateChildValues(["dealResul":dealResult])
Run Code Online (Sandbox Code Playgroud)PS: -尽量使用runTransactionBlock:替代.updateChildValues,如果你只想增加一个特定节点.另请阅读: - /sf/answers/2762063111/
我建议使用setValue单个子更新(请记住我使用的是Swift 3,可能会有轻微的语法更改,但你应该没问题):
rootRef.child("komal_kyz").child(key).setValue(["dealResul":dealResult])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13225 次 |
| 最近记录: |