简体   繁体   English

firebase auth / swift-无法在applicationWillTerminate上删除匿名用户?

[英]firebase auth / swift - anonymous user not delete on applicationWillTerminate?

So on my first loading screen, I have the user sign in anonymously - so they have permission to access the Firebase database. 因此,在第一个加载屏幕上,我让用户匿名登录-因此他们有权访问Firebase数据库。 Here is the code: 这是代码:

Auth.auth().signInAnonymously { (user, error) in
    if error != nil {
        print(error!.localizedDescription)
        SVProgressHUD.showError(withStatus: AlertMessages.authError)
    } else {
        print("successfully signed in anon")
    }
}

successfully signed in anon prints out every time I run it, which is what should happen. 每次运行都会successfully signed in anon打印,这应该发生。 But in my applicationWillTerminate , I try to delete this anonymous user so there won't be any dangling users, but it doesn't work. 但是在我的applicationWillTerminate ,我尝试删除此匿名用户,因此不会有任何悬空的用户,但这是行不通的。 Here is the code: 这是代码:

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    print("will terminate")

    if Auth.auth().currentUser != nil {
        print("user exists")
        if (Auth.auth().currentUser?.isAnonymous)! {
            print("delete anonymous user")
            Auth.auth().currentUser?.delete(completion: { (error) in
                if error != nil {
                    print(error!.localizedDescription)
                    try! Auth.auth().signOut()
                } else {
                    print("delete success")
                }
            })
        }
    }
}

It prints out: 它输出:

will terminate
user exists
delete anonymous user

Why is it that it never prints out the completion block? 为什么它从不打印完成块?

It is very strange. 这很奇怪。 At first glance it seems that you are doing everything right. 乍一看,看来您做对了所有事情。

Try this approach: 试试这种方法:

let user = Auth.auth().currentUser

user?.reauthenticate(with:credential) { error in

    if let error = error {
        showAlertWithErrorMessage(message: error.localizedDescription)
    } else {

        user?.delete { error in

            if let error = error {
                showAlertWithErrorMessage(message: error.localizedDescription)
            } else {

                let userID = HelperFunction.helper.FetchFromUserDefault(name: kUID)
                Database.database().reference(fromURL: kFirebaseLink).child(kUser).child(userID).removeValue()

                try!  Auth.auth().signOut()
                showAlertWithErrorMessage(message: "Your account deleted successfully...")

                return
            }
        }

    }
}

It looks like this is some what of a problem. 看起来这有点问题。 The first question I have is why do you want to delete the user? 我的第一个问题是为什么要删除用户?

Looking at this answer you see that once an anonymous user's authentication token is removed / expired their account if forgotten by Firebase. 查看此答案,您会发现,一旦Firebase忘记了匿名用户的身份验证令牌/他们的帐户已过期,该令牌就会失效。 This means that signing an anonymous user out of their account would remove that user. 这意味着从其帐户中注销匿名用户将删除该用户。 If the same user signed in anonymously again they would be provided a new token. 如果同一用户再次匿名登录,将为他们提供一个新令牌。

The documentation also states: 文档还指出:

To protect your project from abuse, Firebase limits the number of new email/password and anonymous sign-ups that your application can have from the same IP address in a short period of time. 为了保护您的项目免受滥用,Firebase限制了您的应用程序在短时间内可以从同一IP地址获得的新电子邮件/密码和匿名注册的数量。 You can request and schedule temporary changes to this quota from the Firebase console 您可以从Firebase控制台请求并计划对此配额的临时更改

If you're trying to delete accounts and the data associated with those accounts I'd take a look at this repo . 如果您要删除帐户以及与这些帐户相关联的数据,请查看此存储

Looking at Authenticate with Firebase Anonymously and Delete a User documentation there is no mention of deleting an anonymous account. 查看“ 使用Firebase进行匿名身份验证”和“ 删除用户”文档,这里没有提到删除匿名帐户。 I think this is because signing them out is the same thing as deleting a real account. 我认为这是因为退出帐户与删除真实帐户是一回事。 This is probably why calling Auth.auth().currentUser?.delete seems to do nothing on an anonymous user. 这可能就是为什么调用Auth.auth().currentUser?.delete似乎对匿名用户不执行任何操作的原因。

Update 更新资料

OP confirmed that you can delete an anonymous user. OP确认您可以删除匿名用户。 The above code placed inside applicationDidEnterBackground deletes the account. 上面放置在applicationDidEnterBackground中的代码将删除该帐户。 So the question is why doesn't the print statement appear when the code is placed inside applicationWillTerminate . 因此,问题在于,当将代码放置在applicationWillTerminate内时,为什么不会显示print语句。

Looking at applicationWillTerminate : 查看applicationWillTerminate

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. 如果该方法在时间到期之前没有返回,则系统可能会完全终止该过程。

This is probably why you aren't seeing the callback print statement. 这可能就是为什么您没有看到回调打印语句的原因。 The app is terminated before that completion is returned. 该应用程序在完成返回之前终止。 I'm not 100% sure how anonymous sign in works, but there should be other ways to verify the account was deleted. 我不是100%知道匿名登录的工作方式,但是应该有其他方法来验证帐户是否已删除。

After the application is terminated do you have to sign in again? 终止申请后,您是否必须重新登录? If you do then the token was deleted. 如果您这样做,则令牌被删除。

Additionally, the app life cycle documentation states : 此外, 应用程序生命周期文档指出

Apps must be prepared for termination to happen at any time and should not wait to save user data or perform other critical tasks. 应用必须随时准备终止,并且不应该等待保存用户数据或执行其他关键任务。 System-initiated termination is a normal part of an app's life cycle. 系统启动的终止是应用程序生命周期的正常组成部分。 The system usually terminates apps so that it can reclaim memory and make room for other apps being launched by the user, but the system may also terminate apps that are misbehaving or not responding to events in a timely manner. 系统通常会终止应用程序,以便可以回收内存并为用户正在启动的其他应用程序腾出空间,但是系统也可能会终止行为不当或未及时响应事件的应用程序。

Suspended apps receive no notification when they are terminated ; 挂起的应用程序终止时不会收到任何通知 the system kills the process and reclaims the corresponding memory. 系统终止进程并回收相应的内存。 If an app is currently running in the background and not suspended, the system calls the applicationWillTerminate: of its app delegate prior to termination. 如果某个应用程序当前正在后台运行且未挂起,则系统将在终止之前调用其应用程序委托的applicationWillTerminate:。 The system does not call this method when the device reboots. 设备重启时,系统不会调用该方法。

In addition to the system terminating your app, the user can terminate your app explicitly using the multitasking UI. 除了系统终止您的应用程序外,用户还可以使用多任务UI显式终止您的应用程序。 User-initiated termination has the same effect as terminating a suspended app. 用户启动的终止与终止已暂停的应用程序具有相同的效果。 The app's process is killed and no notification is sent to the app. 该应用程序的进程被终止,并且没有通知发送到该应用程序。

Since suspended apps do not receive an applicationWillTerminate notification you might want to reconsider how you are doing things. 由于暂停的应用程序不会收到applicationWillTerminate通知,因此您可能需要重新考虑您的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM