swift - 如何在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#>
})
}
}
谢谢你
解决方案
你得到的价值
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
}
推荐阅读
- c++ - Apache Thrift 在生成的代码中未定义对 vtable 的引用
- javascript - Angularjs 搜索和显示
- c++ - ubuntu 16.04 中的 protoc 共享库错误
- c# - 如何正确地将 CommandParameter 与我的 ContextMenu 绑定到选定的 ListViewItem
- sql - 使用 DISTINCT 语句的 grp 中的唯一值?
- raku - Perl 6 遍历命令行参数
- php - 在 Woocommerce 价格下方添加额外的添加到购物车按钮
- connection-pooling - AgensGraph 是否支持连接池?
- lldb - 如何设置仅在特定父函数中触发的断点?
- c# - 在运行时将 StackPanel 添加到 WPF DataGrid