Swift Firebase嵌套 child 字典删除

标签 swift firebase firebase-realtime-database nested

firebase structure

在 firebase 结构中,您可以看到我必须删除所有组中的特定用户(currentUserId):

这就是我尝试做的:

###########################UPDATED###################################

 let groupsRef = self.root.child("groups")
groupsRef.observeSingleEvent(of: .value, with: { snapshot in
   
    for groupChild in snapshot.children {
        let groupSnap = groupChild as! DataSnapshot
        var dict = groupSnap.value as! [String: Any]
            let uid = dict["utenti"] as! [String: Bool]
        for each in uid {                
            if each.key == self.currentUserID{
                print(each.key)
               
      //i now need a way to remove this key:value

            }
            
            }
           
        
       
    }
   
})

我是新来的,所以我无法进一步提取字典的每个键,而不是与我必须删除的键进行比较,如果相同,我将删除。 有人可以帮忙吗?

最佳答案

          let groupsRef = self.root.child("groups")
                    groupsRef.observeSingleEvent(of: .value, with: { snapshot in

                        for groupChild in snapshot.children {
                            let groupSnap = groupChild as! DataSnapshot
                            let groupKey = groupSnap.key
                            //added a groupKey to track the id of each group
                            var dict = groupSnap.value as! [String: Any]
                            var uid = dict["utenti"] as! [String: Bool]

                            //then for each key:value in uid check if is there a key = to currentuserID
                            for each in uid {

                                if each.key == self.currentUserID{
                                    uid.removeValue(forKey: each.key)
                                  //here you remove currentuserId from the dictionary and below 
                                 //finally you set back the new value of the dictionary without currentuserId

                   self.root.child("groups").child(groupKey).child("utenti").setValue(uid)

                                } 
                            }
                        }    
                    })

关于Swift Firebase嵌套 child 字典删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47797356/

相关文章:

ios - 当应用程序终止时,APN 在委托(delegate)中接收通知数据

ios - Swift Spritekit 计数触摸

java - 如何使用同步块(synchronized block)序列化异步调用

ios - 如何在 tableView 上反射(reflect) childChanges - IOS Swift Firebase

ios - Swift - Firebase 获取所有键并将它们存储到数组中

ios - Alamofire HTTPS 请求不适用于该网站

ios - 在某些操作系统版本中未调用 WKHTTPCookieStore getAllCookies 完成处理程序

angularjs - 如何使用 AngularFire $asObject() 检查 Firebase 中是否存在该对象?

node.js - Firebase:找不到 firebase 命名空间;请务必在此库之前包含 firebase-app.js。我得到了那个错误。我应该如何修复我的代码?

android - 需要始终删除 Firebase 监听器吗?