如何从Firebase数据库中删除 [英] How to delete from Firebase database
问题描述
您好,我已经按照这个教程:
我调用我的函数: remove(child:mixcloudLinks)
如果我想更深入地删除例如added p>
我需要改变一点功能。
static func remove(parentA:String,parentB:String,child:String){
self.ref.child(mixcloudLinks)。child(parentA).child(parentB ).child(child)
ref.removeValue {error,_ in
print(error)
}
}
调用如下:
let parentA =DDD30E1E-8478-AA4E-FF79-1A2371B70700
let parentB =-KSCRJGNPZrTYpjpZIRC
let child =added
remove(parentA:parentA,parentB:parentB,child :child)
这会删除键值added b
编辑
在autoID的情况下,您需要将autoID保存到您的Dictionary中才能使用它比较晚。
这是我的一个函数:
$ p $ func store(uuid:String,item:MyClassObject){
var i tem = item.toJson()
let ref = self.ref.child(uuid).childByAutoId()
item [uuid] = reference.key //这里我是将autoID键保存到字典中,以便稍后可以删除这个项目
ref.setValue(item){error,_ in
if error = error {
print(error)
}
}
}
因为那么我将autoID作为我的字典的一部分,并且可以稍后删除它:
<然后我可以使用它像 .child(MyClassObject.uuid).remove ...
这就是自动生成的ID。
Hi i have followed this tutorial: https://www.youtube.com/watch?v=XIQsQ2injLo
This explains how to save and retrieve from the database, but not how to delete. I am wondering how to delete the database node that belongs to the cell that is being deleted. Thanks
Edit. Code updated for Swift 3 and Swift 4.
I'm always using the remove with completion handler:
static let ref = Database.database().reference()
static func remove(child: String) {
let ref = self.ref.child(child)
ref.removeValue { error, _ in
print(error)
}
}
So for example if I want to delete the following value:
I call my function: remove(child: "mixcloudLinks")
If I want to go deeper and delete for example "added":
I need to change the function a little bit.
static func remove(parentA: String, parentB: String, child: String) {
self.ref.child("mixcloudLinks").child(parentA).child(parentB).child(child)
ref.removeValue { error, _ in
print(error)
}
}
Called like:
let parentA = "DDD30E1E-8478-AA4E-FF79-1A2371B70700"
let parentB = "-KSCRJGNPZrTYpjpZIRC"
let child = "added"
remove(parentA: parentA, parentB: parentB, child: child)
This would delete just the key/value "added"
EDIT
In case of autoID, you need to save the autoID into your Dictionary to be able to use it later.
This is for example one of my functions:
func store(uuid: String, item: MyClassObject) {
var item = item.toJson()
let ref = self.ref.child(uuid).childByAutoId()
item["uuid"] = reference.key // here I'm saving the autoID key into the dictionary to be able to delete this item later
ref.setValue(item) { error, _ in
if let error = error {
print(error)
}
}
}
Because then I'm having the autoID as part of my dictionary and am able to delete it later:
Then I can use it like .child(MyClassObject.uuid).remove...
which is then the automatic generated id.
这篇关于如何从Firebase数据库中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!