【发布时间】:2018-05-08 13:00:23
【问题描述】:
我想删除当前用户或当前 uid 的帖子。发帖用户的 uid 在 JSON 中由 String:uid 指定。所以我在快照中搜索 uid,如果它存在,我删除该 uid 的父级。问题是所有帖子都会被删除,而不仅仅是存储当前 uid 的帖子。
JSON 树
Posts
-LBzjsl0Yfwdyh9xg6D2 (this is random uid)
photoUrl: "https://firebasestorage.googleapis.com/v0/b/blo..."
uid: "BBPHwd0RRibXFCvHcXA2aKbHoTm1" (this is uid of the current user)
代码:
let user = Auth.auth().currentUser?.uid
databaseRef.child("Posts").queryOrdered(byChild: "uid").queryEqual(toValue: user).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
//find out parent of current post
let parent = snapshot.ref.parent!.key
let ref = Database.database().reference()
print (parent)
func remove(child: String) {
ref.removeValue { error, _ in
}
}
remove(child: parent)
【问题讨论】:
-
你能不能更好地解释一下父母和孩子是什么?另外,您到底要删除什么,而要删除什么?
-
请查看这些steps 以帮助澄清您的问题。
-
我想删除当前用户或当前uid的帖子。发帖用户的 uid 在 JSON 中由 String:uid 指定。所以我在快照中搜索 uid,如果它存在,我删除该 uid 的父级。问题是所有帖子都会被删除,而不仅仅是存储当前 uid 的帖子。
-
您是否尝试过移除孩子而不是父母?
标签: swift firebase firebase-realtime-database