表格视图从Firebase中删除

时间:2018-03-10 19:54:54

标签: ios swift uitableview firebase

我已设法将数据添加到表格视图并将其导入特定的Firebase位置。我现在希望能够从应用程序内部删除此数据。

目前,使用此代码,该条目会成功从应用内删除,但不会从Firebase中删除,并且当重新打开该应用时,该条目会返回。基本上我想知道我做错了什么以及我可以改变什么来实际删除实时数据库中的条目。

import UIKit
import Firebase

class WorkoutNotesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {

var addExer:[String] = []
var handle: DatabaseHandle?
var ref: DatabaseReference?
var keyArray: [String] = []

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var exerView: UITextField!
@IBAction func inputButton(_ sender: Any) {

if exerView.text != ""
{
    ref?.child("users").child(Auth.auth().currentUser!.uid).child("notes").childByAutoId().setValue(exerView.text)
        exerView.text = ""
    }
else
    if exerView.text == "" {
    let alertController = UIAlertController(title: "Error", message: "Retry", preferredStyle: .alert)
    let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
    alertController.addAction(defaultAction)
    present(alertController, animated: true, completion: nil)
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return addExer.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    cell.textLabel?.text = addExer[indexPath.row]
    return cell
}

// Do any additional setup after loading the view.
override func viewDidLoad() {
    super.viewDidLoad()

    tableView.allowsMultipleSelectionDuringEditing = true

    ref = Database.database().reference()

handle = ref?.child("users").child(Auth.auth().currentUser!.uid).child("notes").observe(.childAdded, with: { (snapshot) in
    if let item = snapshot.value as? String
    {
        self.addExer.append(item)
        self.tableView.reloadData()
    }
    })

}

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

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    print(indexPath.row)
    if editingStyle == .delete {
        GetAllKeys()
        let when = DispatchTime.now() + 1
        DispatchQueue.main.asyncAfter(deadline: when, execute: {
            self.ref?.child("users").child(Auth.auth().currentUser!.uid).child("notes").child(self.keyArray[indexPath.row])
            self.addExer.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .automatic)
            self.keyArray = []
        })
    }

}

func GetAllKeys() {
    ref?.child("users").child(Auth.auth().currentUser!.uid).child("notes").observeSingleEvent(of: .value, with: { (snapshot) in
        for child in snapshot.children {
            let snap = child as! DataSnapshot
            let key = snap.key
            self.keyArray.append(key)
        }
    })
 }

 }

1 个答案:

答案 0 :(得分:0)

您必须嵌入removeValue()

self.ref?.child("users").child(Auth.auth().currentUser!.uid).child("notes").child(self.keyArray[indexPath.row]).removeValue()

使用完成功能还可以100%确定该项目是否已从firebase中删除

 self.ref?.child("users").child(Auth.auth().currentUser!.uid).child("notes").child(self.keyArray[indexPath.row]).removeValueWithCompletionBlock({ (error, refer) in
   if error != nil {
      print(error)
   } else {
      print("Child Removed successfilly")
  }