【发布时间】:2016-11-29 22:03:41
【问题描述】:
点击删除按钮即可。我正在尝试从我的 Firebase 数据库中删除一个特定的孩子。我的问题是我无法在我的数据库中引用特定的childByAutoId,因此我的应用程序不知道要删除哪个孩子。
Firebase 数据库
数据服务文件
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]
}
我没有在网上找到有关此的信息。提前致谢!
【问题讨论】:
-
你怎么知道你想删除哪个孩子?即我想删除一个值为 'pizza' 的孩子 'favorite_food:'。
-
我想删除整个孩子。例如 -KWKZGKMTjwkyjpQtwPn 及其所有内容
-
基于什么参数?是什么让那个子节点需要被删除?是因为它符合/不符合标准吗?因为它的价值是“披萨”?因为用户刷卡删除了?您需要考虑要删除节点的原因是什么。一旦我们知道这一点,我们就会有答案。
-
@Jay 他想删除它,因为用户删除了 tableView 中的项目。
-
@ChandlerLong 你在哪里用
inventory节点中的孩子填充你的tableView?
标签: swift firebase firebase-realtime-database ios10