iOS Firebase-如何从不同节点删除具有相同密钥的子级 [英] iOS Firebase -How to remove children with same key from different nodes

查看:74
本文介绍了iOS Firebase-如何从不同节点删除具有相同密钥的子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Following的裁判.在该引用下,有2个不同的userId跟随同一用户.如果他们都在关注的用户想要删除其帐户,我想从关注"节点删除它们.多位置更新似乎无法实现这一目标.

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.

怎么办?

用户kk8qFOIw ...是要删除其帐户的用户.删除后,应从其他用户的节点中删除其密钥.

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

推荐答案

这是您可以做到的方式:

This is how you can do it :

首先获取id = 1的所有节点,然后运行多路径更新并将其设置为空.

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

        })
    }

这篇关于iOS Firebase-如何从不同节点删除具有相同密钥的子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆