首页 > 解决方案 > 如何在firebase swift 4中使用childByAutoId获取自动生成的数字

问题描述

我使用 childByAutoId 在我的 firebase 数据库中创建了一个节点。现在我想用swift删除它。如何获得对这个自动生成号码的引用?我正在考虑使用此代码:

func deleteAction(at indexPath : IndexPath) -> UIContextualAction {
    let action = UIContextualAction(style: .destructive, title: "Delete") { (action, view, completion) in
        let vinyl = self.vinyls[indexPath.row]
        guard let userID = Auth.auth().currentUser?.uid else { fatalError() }

        Database.database().reference().child("vinyls").child(userID).removeValue(completionBlock: { (error, _) in
            <#code#>
        })


    }
}

谢谢你

标签: swiftfirebaseswift4

解决方案


你得到的价值

Database.database().reference().child("vinyls").observeSingleEvent(of: .value) { (snapshot) in

   let ref = snapshot.value as! [String:[String:Any]]

   // now you get ref where key is the auto-generated id and value is the dictionary you set with setValue before 

}

推荐阅读