I need a firestore function to execute when the user terminates the app. Specifically, I need to delete a document in firestore. I used the applicationWillTerminate in the AppDelegate for that.
This is the code:
func applicationWillTerminate(_ application: UIApplication) {
print("App terminated")
guard let email = UserDefaults.standard.value(forKey: "email") as? String else {
return
}
let safeEmail = DatabaseManager.safeEmail(emailAddress: email)
Firestore.firestore().collection("LocationsList").document(safeEmail).delete() { err in
if let err = err {
print("Error removing document: \(err)")
}
else {
print("Document removed")
}
}
}
Terminal successfully prints "App terminated" meaning that the function is called when the user terminates the app. However, the Firestore call doesn't completely execute, which I would believe is due to the short time limit the applicationWillTerminate has. Is there any way I can get the Firestore document to delete/the call to execute completely?