Firebase快速删除快照儿童

时间:2016-08-17 13:13:02

标签: swift firebase-realtime-database

我使用Firebase作为我的数据库... enter image description here

然后我想删除"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()崩溃了。

3 个答案:

答案 0 :(得分:4)

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

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!()
       }
   }
})

答案 1 :(得分:2)

Hello there i finally find a solution:

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()

                    }
                }
            }
        })

Thanks a lot!

答案 2 :(得分:0)

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()

                 }
            }
          }
        }