【问题标题】:Firebase remove snapshot children swiftFirebase 快速删除快照子项
【发布时间】:2016-08-19 03:09:47
【问题描述】:

我正在使用Firebase 作为我的数据库...

然后我想删除"codigo" 键值。这是我的 if 语句:

let profile = FIRDatabase.database().reference().child("barcodes")
         profile.observeEventType(.Value, withBlock: { (snapshot) -> Void in


            for item in snapshot.children {

                if item.value["codigo"]as! String == barcodes[indexPath.row].code{
                    print("HERE")

                item.removeValue!()


                }


            }

但它在item.removeValue() 崩溃。

【问题讨论】:

    标签: swift firebase-realtime-database


    【解决方案1】:

    您无法删除快照。但是您可以获取快照来自的引用并将其删除:

    let profile = FIRDatabase.database().reference().child("barcodes")
    profile.observeEventType(.Value, withBlock: { (snapshot) -> Void in
       for item in snapshot.children {
           if item.value["codigo"]as! String == barcodes[indexPath.row].code{
               print("HERE")
               item.ref.removeValue!()
           }
       }
    })
    

    【讨论】:

    • 感谢弗兰克,它删除了我的所有数据,我不希望这样。我只想删除那个特定的条形码。只是快照中的一个特定项目...有什么帮助吗?
    【解决方案2】:

    你好,我终于找到了解决办法:

    let profile = FIRDatabase.database().reference().child("barcodes")
         profile.observeEventType(.Value, withBlock: { (snapshot) -> Void in
    
    
              if snapshot.exists(){
    
                    for item in snapshot.children {
                        if item.value["codigo"]as! String == barcodes[index].code{
    
                            item.ref.child(item.key!).parent?.removeValue()
    
                        }
                    }
                }
            })
    

    非常感谢!

    【讨论】:

      【解决方案3】:
      let profile = FIRDatabase.database().reference().child("barcodes")
           profile.observeEventType(.Value, withBlock: { (snapshot) -> Void in
            if snapshot.exists(){
      
              if let item = snapshot.value as? [String:AnyObject]{
                for each in item.1 as [String : AnyObject]{
      
                 let barcodeKey = each.0
                  if each.1["codigo"] as! String == barcodes[indexPath.row].code{
      
                        FIRDatabase.database().reference().child("barcodes").child(barcodeKey)child("codigo").removeValue()
      
                       }
                  }
                }
              }
      

      【讨论】:

      • 您好 Dravidian,感谢您的转发,但它仍然没有在我的 firebase 控制台上删除,并且它崩溃了。代码有一些错误,你测试了吗?在此先感谢,如果您不能仅删除 codigo 密钥,您可以帮我删除此“codigo”的 AutoId,
      • 我在 if item.1["codigo"]as 中也有错误! String == 条码[indexPath.row].code
      • 他们都在打印相同的字符串,但在我的条形码数组计数后崩溃...
      • 是的,我尝试过@Frank van Puffelen 的回答,但它会删除所有条形码,我不希望这样。 barcodeKey 打印“条形码”
      • Dravidian 它终于被删除了我使用了这个代码:FIRDatabase.database().reference().child("barcodes").child(item.key!).removeValue()。但是他正在删除我所有的数据库,我该怎么办?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      相关资源
      最近更新 更多