Ios Swift Firebase在应用程序终止时删除用户

Ios Swift Firebase在应用程序终止时删除用户,ios,swift,firebase,Ios,Swift,Firebase,当应用程序终止时,我试图删除用户,但这不起作用。 如果我有一个匿名用户登录,并且该用户关闭了应用程序,我不希望firebase保存该匿名用户。我想删除它。这是我当前的代码。先谢谢你 func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also

当应用程序终止时,我试图删除用户,但这不起作用。 如果我有一个匿名用户登录,并且该用户关闭了应用程序,我不希望firebase保存该匿名用户。我想删除它。这是我当前的代码。先谢谢你

func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        if let users = Auth.auth().currentUser {
            if !users.isAnonymous {
              return
            }else {
                users.delete(completion: { (error) in
                    if error != nil {
                        print(error!)
                        return
                    }
                    try! Auth.auth().signOut()
                    print("Anonymous user deleted")
                })
            }
        }
    }

Hei@pprevalon,我一直在努力实现与您相同的目标,只需更新appWillTerminate上的值,但以下是我从其他成员那里了解到的

此方法的实现大约有五秒钟的时间来执行任何任务并返回。如果该方法在时间到期之前未返回,系统可能会完全终止该进程

仍然在试图找到一种方法,因为调用func appWillTerminate会发生1/10次,但我不能指望这一点


另外,请考虑一下:如果用户首先从active->background切换,然后从background->kill app切换,则该方法将永远不会被调用。

我将ViewDidLoad中的侦听器添加到我希望删除该用户的屏幕中。像这样:

    NotificationCenter.default.addObserver(self, selector: #selector(suspending), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(suspending), name: NSNotification.Name.UIApplicationWillTerminate, object: nil)
然后我有这个功能来删除用户:

  @objc func suspending () {
    if Auth.auth().currentUser != nil {
        let user = Auth.auth().currentUser
        user?.delete { error in
            if let error = error {
                print("An error happened delting the user.")
            } else {
                print("Account succesfully deleted.")
            }
        }
    }
}

唯一可能发生的事情是,您需要重新验证。如果是这样的话,那么也可以按照下面的步骤来实现它

运行此操作时会发生什么?有错误吗?或者它表现出什么样的行为?完全没有错误。如果我在
user.delete
下面添加一条print语句,该print语句将被打印,但user.delete将不会执行。我在想一个解决方案。你知道用户注销是否会触发删除匿名用户,或者在我删除它之前它是否存在。你是否将匿名用户添加到你的数据库中?经过反复研究,我得出结论,Firebase目前不支持删除匿名用户。有关更多信息,请参阅创建一个真实的虚拟用户,将他们登录到您的应用程序中,然后运行删除以查看是否调用了该方法。如果执行了,那么FrB仍然不支持匿名删除。