Mag*_*nus 8 firebase swift firebase-realtime-database
您好我遵循了本教程:https://www.youtube.com/watch?v = XIQsQ2injLo
这解释了如何从数据库中保存和检索,而不是如何删除.我想知道如何删除属于正在删除的单元格的数据库节点.谢谢
Dav*_*eek 27
编辑.代码更新为Swift 3和Swift 4.
我总是使用remove with completion处理程序:
static let ref = Database.database().reference()
static func remove(child: String) {
let ref = self.ref.child(child)
ref.removeValue { error, _ in
print(error)
}
}
Run Code Online (Sandbox Code Playgroud)
例如,如果我想删除以下值:
我叫我的功能: remove(child: "mixcloudLinks")
如果我想更深入并删除例如"添加":
我需要稍微改变一下这个功能.
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)
}
}
Run Code Online (Sandbox Code Playgroud)
被称为:
let parentA = "DDD30E1E-8478-AA4E-FF79-1A2371B70700"
let parentB = "-KSCRJGNPZrTYpjpZIRC"
let child = "added"
remove(parentA: parentA, parentB: parentB, child: child)
Run Code Online (Sandbox Code Playgroud)
这将删除"添加"的键/值
编辑
如果是autoID,您需要将autoID保存到Dictionary中以便以后使用它.
这是我的一个功能:
func store(item: MyClassObject) {
var item = item.toJson()
let ref = self.ref.child("MyParentFolder").childByAutoId()
item["uuid"] = ref.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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
因为那时我将autoID作为我字典的一部分,以后可以删除它:
然后我可以使用它.child(MyClassObject.uuid).remove...,然后自动生成的ID.
| 归档时间: |
|
| 查看次数: |
14325 次 |
| 最近记录: |