2016-08-17 110 views
0

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

回答

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

        } 
       } 
      } 
     }) 

非常感謝!

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

       } 
      } 
      } 
     } 
+0

你好,Dravidian,感謝您的轉發,但它仍然沒有刪除我的firebase控制檯,它崩潰。代碼有一些錯誤,你測試它嗎?在此先感謝,如果你不能刪除只是codigo密鑰,你可以幫我刪除這個「codigo」的自動識別號, –

+0

我有一個錯誤在snapshot.exist()函數 –

+0

也我有一個錯誤,如果item.1 [ 「codigo」]作爲! String ==條形碼[indexPath.row] .code –

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

謝謝弗蘭克它刪除,但我所有的數據,我不希望。我想刪除只有一個特定的條形碼。只是快照中的一個特定項目...有幫助嗎? –