0

So the weird thing is App Delegate is run at start of the app, so why is this App Delegate showing up when you want to delete a user. I assume this might have something to do with the user being removed and the app still expecting something of him to be there. But then when I check in my database nothing from the user is even removed before it crashes.

Below is the code that executes when the user presses delete. In words, what it should doing in order is: 1) remove UID from realtime database references, 2) remove UID from storage, 3) remove UID from authentication.

 let deleteDataGroup = DispatchGroup()

@IBAction func deleteAccount(_ sender: Any) {
    let databaseRef = Database.database().reference()
    let uid = Auth.auth().currentUser!.uid
    deleteDataGroup.enter()

    databaseRef.child("A11").child(uid).removeValue(){ error, _ in
        if let error = error { print(error) }
    }
    databaseRef.child("A22").child(uid).removeValue(){ error, _ in
        if let error = error { print(error) }
    }
    databaseRef.child("Loc1").child(uid).removeValue(){ error, _ in
        if let error = error { print(error) }
    }
    databaseRef.child("users").child(uid).removeValue(){ error, _ in
        if let error = error { print(error) }
    }
    databaseRef.child("switch").child(uid).removeValue(){ error, _ in
        if let error = error { print(error) }
    }
    deleteDataGroup.leave()

    let user = Auth.auth().currentUser
    deleteDataGroup.enter()

    Storage.storage().reference().child("images").child(uid).listAll { list, error in
        if let error = error { print(error) }
        list.items.forEach({ file in
            file.delete { error in
                if let error = error { print(error) }
            }
        })
        self.deleteDataGroup.leave()

    }

    deleteDataGroup.notify(queue: .main) {

           Auth.auth().currentUser?.delete()
        { error in
            if error != nil {
              print("an error deleting")
            // An error happened.
          } else {
              print("deleted")
              self.performSegue(withIdentifier: "delete", sender: nil)

            // Account deleted.
          }
        }
    }

    
}

App Delegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()
    Database.database().isPersistenceEnabled = true
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])   {
let databaseRef = Database.database().reference()
....
}
florida27
  • 67
  • 1
  • 7
  • The title says an error occurs: *persistence must be enabled* but the body asks *App Delegate showing up when you want to delete a user* but no mention of what the AppDelegate has to do with the question. The removal of nodes from the RTDB can be done with a [Multi Location Delete](https://stackoverflow.com/questions/38462074/using-updatechildvalues-to-delete-from-firebase/38466959#38466959); so a DispatchGroup is not needed or for removing data from Storage. Also, there's no code in the question that actually deletes a user. Can you clarify what the actual issue is and which line is crashing? – Jay Feb 14 '22 at 19:26
  • [1]`isPersistenceEnabled` is already called in App Delegate. That is why I said that. I am adding App Delegate to the question. [2]Regarding no code that deletes a user: I have updated that in the question in the 'deleteDataGroup.notify'. [3]The line that crashes would be the first removal, ie `databaseRef.child("A11").child(uid).removeValue()` How do I know that? Because I changed that line to `setValue` to `""` instead of `removeValue()` and it passed. Then it crashed in the next removal, ie `databaseRef.child("A22").child(uid).removeValue()`. – florida27 Feb 15 '22 at 09:41
  • The crash is likely caused by the `uid` var being nil. That would be because that optional is being force unwrapped here `let uid = Auth.auth().currentUser!.uid` and that's bad practice. Protect your code by handling situations where the optional is nil before trying to use it `guard let currentUser = Auth.auth().currentUser else { print error message and return }` Please check that var - I suggest adding a breakpoint and stepping through your code line by line, inspecting the vars and code execution along the way. You can quickly nail down where your crash is using that technique. – Jay Feb 15 '22 at 18:04

0 Answers0