I have app which uses firebase for database and I want to clear specific user field called current_venue when user kill the app and for that I am using AppDelegate method applicationWillTerminate and it's actually calling when I kill the app but not completely.
Below is my code in AppDelegate:
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
WebServiceAPI.checkOutVenue()
}
And another method is:
static func checkOutVenue() {
WebServiceAPI.sharedInstance.current_venue = ""
if WebServiceAPI.sharedInstance.user == nil {
return
}
let user = WebServiceAPI.sharedInstance.user!
let ref = FIRDatabase.database().reference()
ref.child("Clients").child(user.uid).child("current_venue").setValue("") { (err, venueIdRef) in
if err == nil {
WebServiceAPI.sharedInstance.current_venue = ""
}
}
}
And I tried to debug it something like below image:
When I kill app breakpoint 1 and 2 is calling but its never reaching at breakpoint 3.
I read the documentation from:
https://developer.apple.com/reference/uikit/uiapplicationdelegate/1623111-applicationwillterminate
Which says:
Your implementation of this method has approximately five seconds to perform any tasks and return. If the method does not return before time expires, the system may kill the process altogether.
And Actually its not taking more than 5 seconds to clear that field from firebase.
Same code is working fine when I open the app.
So my code is working fine but there is something which I am missing when I am killing the app.
Thanks for your help.

A better way to ensure data is cleaned up when the user disconnects is to use
onDisconnecthandlers. The Firebase documentation explains them, but here's a simple example of how you'd use it:When this code executes, the instruction is sent to the server to remove the node when the client disconnects. That means that it will always run, even when the client (or even the phone) crashes and your client-side code never gets a chance to execute.