2016-11-29 44 views
0

點擊刪除按鈕。我正嘗試從我的Firebase數據庫中刪除特定的子項。我的問題是,我無法參考我的數據庫中的特定childByAutoId,因此我的應用程序不知道要刪除的孩子。如何檢查自動ID子項下的Firebase中的值?

火力地堡數據庫

Firebase Database

DataService的文件

import Foundation 
import Firebase 
import UIKit 

let DB_BASE = FIRDatabase.database().reference().child("laboratory")  //contains the root of our database 
let STORAGE_BASE = FIRStorage.storage().reference() 

class DataService { 

static let ds = DataService() 

//DB References 
private var _REF_BASE = DB_BASE 
private var _REF_STATION = DB_BASE.child("stations") 
private var _REF_USERS = DB_BASE.child("users") 


//Storage Reference 
private var _REF_ITEM_IMAGE = STORAGE_BASE.child("item-pics") 

var REF_BASE: FIRDatabaseReference { 

    return _REF_BASE 

} 

var REF_STATION: FIRDatabaseReference { 

    return _REF_STATION 

} 

var REF_USERS: FIRDatabaseReference { 

    return _REF_USERS 

} 

var REF_ITEM_IMAGES: FIRStorageReference { 

    return _REF_ITEM_IMAGE 

} 

//creating a new user into the firebase database 

func createFirebaseDBUser(_ uid: String, userData: Dictionary<String, String>) { 
    REF_USERS.child(uid).updateChildValues(userData) 
} 

} 

,我試圖刪除這些信息是出了tableViewCell的。我已經嘗試追加到數組的鍵,但我仍然無法訪問適當的子進行刪除。

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
    return true 
} 

func tableView(tableView: (UITableView!), commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: (NSIndexPath!)) { 

} 


func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 

    let currentStation = station.title 
    let stationRef = DataService.ds.REF_STATION.child(currentStation!) 
    let inventoryRef = stationRef.child("inventory") 

    var deleteAction = UITableViewRowAction(style: .default, title: "Delete") {action in 

      //delete items from firebase 


      self.items.remove(at: indexPath.row) 
      tableView.deleteRows(at: [indexPath as IndexPath], with: .fade) 

    } 

    var editAction = UITableViewRowAction(style: .normal, title: "Edit") { action in 



    } 

    return [deleteAction, editAction] 
} 

我在網上找不到關於此的信息。提前致謝!

+0

*您*知道哪個孩子你想刪除嗎?即我想刪除一個孩子'favorite_food:'其價值是'披薩'。 – Jay

+0

我想刪除整個孩子。例如-KWKZGKMTjwkyjpQtwPn a nd其所有內容 –

+0

基於什麼參數?是什麼使得該子節點需要被刪除?是因爲它符合/不符合標準嗎?因爲它的價值是'披薩'?因爲用戶刷卡刪除它?您需要考慮要刪除節點的原因是什麼。一旦我們知道了,我們會有一個答案。 – Jay

回答

0

讓我們開始簡化的結構

-Uiiasidasod 
    item_name: "iMac" 
-Yiimamsmdd 
    item_name: "MacBook" 

典型的設計模式是從火力地堡讀取這些物品,並將它們存儲在字典的陣列。該數組用作tableViews的數據源

每個字典是一個關鍵字:值對。

所以從上述第一節點具有火力地堡鍵「Uiiasidasod」的(父節點的名稱和值「ITEM_NAME:iMac的」(注意值是字典以及)

myArray[0] = ["Uiiasidasod": "item_name: 'iMac'"] 
myArray[1] = ["Yiimamsmdd": "item_name: 'MacBook'"] 

所以當用戶在表中的第1行滑動,您從數組第1行中獲取字典。您可以獲取關鍵部分(Firebase節點名稱),然後可以從Firebase中將其刪除。

相關問題