【问题标题】:How to get hold of a auto generated number with childByAutoId in firebase swift 4如何在firebase swift 4中使用childByAutoId获取自动生成的数字
【发布时间】:2019-04-02 22:58:42
【问题描述】:

我使用 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#>
        })


    }
}

谢谢

【问题讨论】:

    标签: swift firebase swift4


    【解决方案1】:

    你得到了价值

    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 
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      相关资源
      最近更新 更多