【问题标题】:How can I delete a specific child node in Firebase from UITableViewCell using Swift如何使用 Swift 从 UITableViewCell 中删除 Firebase 中的特定子节点
【发布时间】:2016-11-15 14:57:40
【问题描述】:

我有一个UITableView,看起来像这张图片

.

当我滑动删除记录时,我可以将它从存储它的数组中完全删除,但是我在 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 值吗?

【问题讨论】:

  • 这一切都取决于您如何填充数据源数组。请参阅我的答案,这可能会简化它。

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

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

为了删除该值,我创建了对子节点“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)
    }
}

【讨论】:

    【解决方案2】:

    根据 MHDev 已经回答的内容:

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

    【讨论】:

      【解决方案3】:

      这是一个相当简单的过程:

      一般来说,tableViews 的数据源是一个数组。该数组是根据从 Firebase 快照中读取的字典构建的 - 或从快照构建的对象数组(推荐)。

      这是一个与您的 Firebase 结构相匹配的示例(这是从快照中的单个节点填充的)

      class Exercise {
          key: "KWc7RTuOe5PefiMM2tL"
          bodyPart: "Legs"
          exerciseName: "Test 3 "
          userId: "8rHmyTxdocTEvk1ERiiavjMUYyD3"
      }
      

      然后,例如,当用户滑动第 3 行时,从数组 row3 中检索锻炼对象。

      let theObject = ExerciseArray[3]
      let parentNode = theObject.key
      let ref = rootNode.child(parentNode)
      ref.setValue(nil)
      

      你就完成了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-29
        • 1970-01-01
        • 2020-01-29
        • 2021-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多