2016-11-29 43 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中将其删除。

相关问题