【问题标题】:Swift Firebase Delete An user when app will terminateSwift Firebase 在应用程序终止时删除用户
【发布时间】:2017-12-13 10:14:48
【问题描述】:

我试图在应用程序终止时删除用户,但这不起作用。 如果我有一个匿名用户登录并且用户关闭了应用程序,我不希望 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")
                })
            }
        }
    }

【问题讨论】:

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

标签: ios swift firebase


【解决方案1】:

我将 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.")
            }
        }
    }
}

唯一可能发生的情况是,您需要重新进行身份验证。如果是这样的话,那就跟着this answer去实现吧

【讨论】:

    【解决方案2】:

    嘿@pprevalon,我一直在努力实现和你一样的目标,只是通过更新 appWillTerminate 上的值,但这是我从其他成员那里发现的

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

    仍在尝试找出一种方法,因为调用 func appWillTerminate 发生了 1/10 次,但我不能指望这一点。

    附:除此之外,考虑一下:如果用户首先从活动 -> 后台切换,然后是后台 -> 杀死应用程序,则该方法将永远不会被调用。

    【讨论】:

      猜你喜欢
      • 2022-01-27
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      相关资源
      最近更新 更多