【问题标题】:Table View Delete from Firebase从 Firebase 中删除表视图
【发布时间】:2023-04-09 05:15:02
【问题描述】:

我已设法将数据添加到表格视图并将其导入到特定的 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)
        }
    })
 }

 }

【问题讨论】:

    标签: ios swift uitableview firebase


    【解决方案1】:

    你必须嵌入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")
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-25
      相关资源
      最近更新 更多