从Firebase数据库Swift 3中删除子数据

时间:2017-03-20 19:32:04

标签: ios firebase swift3 firebase-realtime-database

请尽可能帮助我。当用户使用removeValue方法调用delete editingStyle函数时,我想删除tableView中的数据。到目前为止,我有一个项目类:

class Item : NSObject {

    var itemName: String!
    var itemDate: String!
    var itemID: String!

    init(itemName: String, itemDate: String, itemID: String) {

        self.itemName = itemName
        self.itemDate = itemDate
        self.itemID = itemID



    }

    init(snapshot: FIRDataSnapshot) {

        let snapshotValue = snapshot.value as? NSDictionary

        self.itemName = snapshotValue!["itemName"] as! String
        self.itemDate = snapshotValue!["itemDate"] as! String
        self.itemID = snapshotValue!["itemID"] as! String
    }

        func toAnyObject() -> [String: AnyObject] {
            return ["itemName": itemName as AnyObject, "itemDate": itemDate as AnyObject, "itemID": itemID as AnyObject]
    }


}

现在我要删除用户itemID中的值,但我不知道从哪里开始:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    guard let uid = FIRAuth.auth()?.currentUser?.uid else {
        return
    }

    let item = itemArray[indexPath.row]
    let itemID = databaseRef.child("ItemID")

    databaseRef.child("users").child(uid).child("usersList").child(itemID).removeValue()

1 个答案:

答案 0 :(得分:0)

我认为你错过了"删除"编辑风格&参考需要一点改变。试试这个:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    guard let uid = FIRAuth.auth()?.currentUser?.uid else {
        return
    }

    let item = items[indexPath.row]
    let itemID = ref.child("users").child(uid).child("usersList").child("ItemID")

    if editingStyle == .delete {
        item.ref?.removeValue()
    }

}