iOS Firebase -How to remove children with same key from different nodes

左心房为你撑大大i 提交于 2019-12-31 05:13:26

问题


I have a ref named Following. Under that ref there are 2 different userIds who are following the same user. If the user they are both following wants to delete their account I want to delete them from the Following node. Multi location update doesn't seem correct to achieve this.

How can it be done?

User kk8qFOIw... is the user who is deleting their account. Once deleted their keys should be removed from the other user's nodes.


回答1:


This is how you can do it :

First get all the nodes where your id = 1 , then run a multipath update and set them to empty.

 let userId = "yourUserId"
    self.ref.child("following").queryOrdered(byChild: userId).queryEqual(toValue: 1).observeSingleEvent(of: .value) { (snasphot) in
        guard let value = snasphot.value as? [String : Any] else {return}


        var multipathUpdate = [String:Any]()
        value.keys.forEach({ (key) in
            multipathUpdate["following/"+key+"/"+userId] = [:]
        })
        self.ref.updateChildValues(multipathUpdate, withCompletionBlock: { (err, ref) in

        })
    }


来源:https://stackoverflow.com/questions/51701778/ios-firebase-how-to-remove-children-with-same-key-from-different-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!