我有一个使用firebase作为数据库的应用程序,我想清除current_venue用户杀死应用程序时调用的特定用户字段,为此我正在使用AppDelegate方法applicationWillTerminate,它实际上是在我杀死应用程序时调用但不完全。
下面是我的代码AppDelegate:
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
WebServiceAPI.checkOutVenue()
}
另一种方法是:
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 = ""
}
}
}
我尝试调试它,如下图所示:
当我杀死应用程序断点 1 和 2 时正在调用,但它从未到达断点 3。
我从以下位置阅读了文档:
https://developer.apple.com/reference/uikit/uiapplicationdelegate/1623111-applicationwillterminate
其中说:
您对此方法的实现大约有五秒钟的时间来执行任何任务并返回。如果该方法在时间到期之前没有返回,系统可能会完全终止该进程。
实际上,从火力基地清除该领域的时间不超过 5 秒。
当我打开应用程序时,相同的代码工作正常。
所以我的代码工作正常,但是当我杀死应用程序时我缺少一些东西。
谢谢你的帮助。
