gpt4 book ai didi

ios - 如何使用 Swift 从 UITableViewCell 中删除 Firebase 中的特定子节点

转载 作者:搜寻专家 更新时间:2023-11-01 06:17:46 25 4
gpt4 key购买 nike

我有一个 UITableView 看起来像这个图像

this image .

当我滑动以删除记录时,我可以将其从存储它的数组中完全删除,但我无法在 Firebase 中访问它以将其删除。

对于上面的截图,我的 Firebase 数据库结构如下:

 -KWc7RTuOe5PefiMM2tL
bodyPart: "Arms"
exerciseName: "Test 1 "
userId: "8rHmyTxdocTEvk1ERiiavjMUYyD3"
-KWcEbpw_f6kxcePY5cO
bodyPart: "Chest"
exerciseName: "Test 2 "
userId: "8rHmyTxdocTEvk1ERiiavjMUYyD3"
-KWcEdUN49QaJIVf0kwO
bodyPart: "Legs"
exerciseName: "Test 3 "
userId: "8rHmyTxdocTEvk1ERiiavjMUYyD3"
-KWcFrMSaLKQRxghGHyT
bodyPart: "Arms"
exerciseName: "Test 4"
userId: "8rHmyTxdocTEvk1ERiiavjMUYyD3"

如何访问创建时设置的 autoId 值,例如“-KWc7RTuOe5PefiMM2tL”,以便删除该子节点?

或者我可以根据登录的 UserId 访问 exerciseName 值吗?

最佳答案

为了解决这个问题,我尝试了多种不同的方法,最终达到了我想要的结果。

为了删除该值,我创建了对子节点“userExercises”的引用,然后按“exerciseName”对其进行排序,然后 .queryEqual(toValue:) 我从 UITableViewCell 中提取的练习名称值。

然后我删除了它的快照值,示例代码如下:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {

if let exerciseName = exercises[indexPath.row].exerciseName {

let ref = FIRDatabase.database().reference().child("userExercises")

ref.queryOrdered(byChild: "exerciseName").queryEqual(toValue: exerciseName).observe(.childAdded, with: { (snapshot) in

snapshot.ref.removeValue(completionBlock: { (error, reference) in
if error != nil {
print("There has been an error:\(error)")
}
})

})

}

exercises.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .left)
}
}

关于ios - 如何使用 Swift 从 UITableViewCell 中删除 Firebase 中的特定子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40612987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com