title: Deleting Firebase values on a SwiftUI List
body: <p>I am trying to delete a value from the Firebase Realtime Database, just by swiping on a the list row but I am encountering a lot of problems, for example, when I delete a row, on the Firebase Realtime Database an object with a different id is deleted, I have the following code running when the swipe occurs (Sorry for the lot of prints but I was trying to understand the flow on my program :D):</p>
  <p>EDIT: I am wondering how I can retrieve the value of the row in the List and by that using this extension:</p>
  <pre><code>extension Dictionary where Value: Equatable {
      func someKey(forValue val: Value) -&gt; Key? {
          return first(where: { $1 == val })?.key
      }
  }
  </code></pre>
  <p>And this is the delete code.</p>
  <pre><code>.onDelete(perform: { indexSet in
                                  locali.remove(atOffsets: indexSet)
                                  Auth.auth().addStateDidChangeListener { auth, user in
                                      print(auth.currentUser?.email)
                                      print(indexSet.count)
                                      print(indexSet.startIndex)
                                      print(indexSet.endIndex)
                                      var indexCounter: Int = indexSet.first!
                                      //MARK: - delete
                                      ref
                                          .child(&quot;locali&quot;)
                                          .child(auth.currentUser?.uid ?? &quot;no uid&quot;)
                                          .child(&quot;nomeLocale\(indexCounter)&quot;)
                                          .removeValue (completionBlock: { (error, refer) in
                                              if error != nil {
                                                  print(error)
                                              } else {
                                                  print(refer)
                                                  print(&quot;Child Removed Correctly&quot;)
                                              }
                                          })
                                      //MARK: - refactor
                                      Task {
                                          var dataSnapshot: DataSnapshot
                                          var refactoredWholeList: [String : NSObject]
                                          do {
                                              dataSnapshot = try await ref
                                                  .child(&quot;locali&quot;)
                                                  .child(auth.currentUser?.uid ?? &quot;no uid&quot;)
                                                  .getData()
                                              print(dataSnapshot)
                                              guard var wholeList = dataSnapshot.value as? [String : NSObject] else {
                                                  print(&quot;error with the guard first inside onDelete&quot;)
                                                  return
                                              }
                                              var indexCounterDelete: Int = 0
                                              
                                              print(&quot;before changing indexes ----&gt; \(wholeList) &lt;----\n&quot;)
                                              for indexDelLocale in wholeList {
                                                  print(&quot;This is the indexDelLocale ==&gt; \(indexDelLocale)&quot;)
                                                  print(&quot;indexCounterDelete: \(indexCounterDelete)&quot;)
                                                  if var entry = wholeList.removeValue(forKey: indexDelLocale.key) {
                                                      print(&quot;Trying to sobstitute the index: \(indexDelLocale.key)&quot;)
                                                      print(&quot;With: \(&quot;nomeLocale\(indexCounterDelete)&quot;) for ==&gt; \(entry)&quot;)
                                                      try await ref
                                                          .child(&quot;locali&quot;)
                                                          .child(auth.currentUser?.uid ?? &quot;no uid&quot;)
                                                          .updateChildValues([&quot;nomeLocale\(indexCounterDelete)&quot; : entry])
                                                  }
                                                  print(&quot;List count ==&gt; \(wholeListRefactored.count)\n&quot;)
                                                  if wholeList.count == indexCounterDelete {
                                                      print(&quot;RETURN&quot;)
                                                  } else {
                                                      indexCounterDelete += 1
                                                  }
                                              }
                                              print(&quot;\nafter changing indexes ----&gt; \(wholeList) &lt;----&quot;)
                                              print(&quot;This is the wholeListRefactored ==&gt; \(wholeListRefactored)\n&quot;)
  
                                          } catch {
                                              print(&quot;error while retrieving the wholeList&quot;)
                                          }
                                      } //: Task
  //                                    //MARK: - reupload
  //                                    for locale in wholeListRefactored {
  //                                        ref
  //                                            .child(&quot;locali&quot;)
  //                                            .child(auth.currentUser?.uid ?? &quot;no uid&quot;)
  //                                            .updateChildValues([locale.key : locale.value])
  //                                    }
                                  }
                              }) //: onDelete
  </code></pre>