简体   繁体   English

从Firebase Database Swift 3删除子数据

[英]Removing child data from Firebase Database Swift 3

Please help me if possible. 如果可以的话请帮助我。 I want to remove data in the tableView when user calls the delete editingStyle function using the removeValue method. 当用户使用removeValue方法调用delete editStyle函数时,我想在tableView中删除数据。 So far I have a class for the item: 到目前为止,我有一个关于该项目的课程:

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]
    }


}

Now I want to delete the values inside the users itemID but I'm not sure where to go from here: 现在,我想删除用户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()

I think you're missing the "delete" editing style & the reference needed a little change. 我认为您缺少“删除”编辑样式,并且参考文献需要进行一些更改。 Try this: 尝试这个:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM