0

I am using Firebase as my Database... enter image description here

Then i want to delete the "codigo" key value. This is my if statement:

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


                }


            }

but it crashes at item.removeValue().

Dravidian
  • 9,945
  • 3
  • 34
  • 74
Kika_Fortez
  • 314
  • 3
  • 10

3 Answers3

4

You cannot remove a snapshot. But you can get the reference that the snapshot comes from and remove that:

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!()
       }
   }
})
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks Frank it removes, but ALL of my Data, and i don't want that. I want to remove just that one specific barcode. Just that one specific item inside of snapshot... Any help? – Kika_Fortez Aug 17 '16 at 15:40
3

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!

Kika_Fortez
  • 314
  • 3
  • 10
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()

                 }
            }
          }
        }
Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • Hello Dravidian, thanks for the repost but it still do no remove on my firebase console, and it crashes. The code have some errors, do you test it? Thanks in advance, if you can't remove just the codigo key you can help me remove the AutoId of this "codigo", – Kika_Fortez Aug 17 '16 at 14:23
  • also i have an error in if item.1["codigo"]as! String == barcodes[indexPath.row].code – Kika_Fortez Aug 17 '16 at 15:31
  • Both of them is printing the same string but it crashes after the number of count of my barcodes array... – Kika_Fortez Aug 17 '16 at 15:51
  • yes i had tried @Frank van Puffelen answer but it removes all the barcodes, and i don't want that. The barcodeKey prints "barcodes" – Kika_Fortez Aug 17 '16 at 16:37
  • Dravidian it is finally removing i used this code : FIRDatabase.database().reference().child("barcodes").child(item.key!).removeValue(). But he is removing all of my database, what i do? – Kika_Fortez Aug 17 '16 at 17:09
  • i have edited my answer , did you try that. also it will because item.key is `barcodes` and you are asking Firebase to remove node `barcodes` which is practically all of your database :) – Dravidian Aug 17 '16 at 17:14