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