0

Im trying to Delete user when app will terminate but this does not work. If I have an anonymous user signed in and the user close the app I don't want firebase to save the anonymous user. I want to delete it. This is my current code. Thank you in advance

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")
                })
            }
        }
    }
KENdi
  • 7,576
  • 2
  • 16
  • 31
SwiftER
  • 1,235
  • 4
  • 17
  • 40
  • what happens when u run this? Any errors? Or what behavior does it exhibit? – eshirima Jul 10 '17 at 14:08
  • no errors at all. If I add a print statement below the `user.delete` that print statement will print but the user.delete will not execute. Im think of a solution Do you know if user signout will fire delete anonymous user or is it there until i delete it. – SwiftER Jul 10 '17 at 21:50
  • are you adding the anonymous users to your database? – eshirima Jul 10 '17 at 21:52
  • After fiddling around, I came to the conclusion that Firebase currently doesn't support the deletion of anonymous users. See [this](https://stackoverflow.com/questions/38694015/what-happens-to-firebase-anonymous-users) for more info – eshirima Jul 10 '17 at 22:06
  • Create a real dummy user, log them into your app then run the deletion to see if the method is called. If it is executed, then FrB still doesn't support anonymous deletion. – eshirima Jul 10 '17 at 22:08
  • actually it does. it runs perfect any other time. maybe when `func applicationWillTerminate()` is initiated ios will cut off web connection to the app immediately. I will check if there is connection to firebase – SwiftER Jul 10 '17 at 22:12
  • Please do keep me updated.. I didn't know applicationwillterminate cuts network connections – eshirima Jul 10 '17 at 22:19
  • I just check to see if i can get a connection status to firebase but that did not print a result. so im assuming firebase cant tell. I will be contacting google for a solution. Its pointless to have all of these anonymous users when store in authentication when they will never be used. – SwiftER Jul 10 '17 at 22:32

2 Answers2

1

I added listerners in the ViewDidLoad to the screen where I want the user the be deleted from. Like this:

    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)

And then I have this function to delete the user:

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

The only thing that could happen is, that you need to re-authenticate. If that's the case, than follow this answer to implement it as well

Vasco
  • 837
  • 8
  • 9
0

Hei @pprevalon, I've been trying to achieve the same thing as you, just by updating a value on appWillTerminate, but here's what I found out from other members

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.

Still trying to figure out a way, since it happens 1/10 times for the func appWillTerminate to be called, but I cannot count on that.

P.S. Besides that, think about this : If the user switches from active -> background at first, followed by background -> kill app, the method will never get called.