ios - 删除用户后应用程序崩溃

标签 ios swift firebase

所以在我的应用程序中,我只是让用户可以选择删除他们的帐户,而且效果很好。但是,删除帐户后,应用程序崩溃了。我认为这是因为它正在尝试搜索用户但不存在。

这是我的代码:

let loginController = LoginController()

func deleteAccount() {

    let user = Auth.auth().currentUser
    let userId = Auth.auth().currentUser?.uid
    let databaseUser = Database.database().reference().child("users").child(userId!)

    user?.delete { error in
        if let error = error {
            print(error)
        } else {
            self.present(self.loginController, animated: true, completion: nil)
        }

    }

    databaseUser.removeValue(completionBlock: { (error, ref) in

        if error != nil {

            print(error)
        } else {



        self.present(self.loginController, animated: true, completion: nil)


        } //Without doing this the user's account only gets deleted in the Authentication, not the whole database. I think this is the problem here?

    })

}

提前致谢!

最佳答案

首先,您需要删除用户数据库,为此您需要 let userId = Auth.auth().currentUser?.uid,只有当用户本身在您的后端,然后你继续删除auth

func deleteAccount() {

let user = Auth.auth().currentUser
let userId = Auth.auth().currentUser!.uid
let databaseUser = Database.database().reference().child("users").child(userId)

databaseUser.removeValue(completionBlock: { (error, ref) in

    if error != nil {

        print(error)

    } else {

       user?.delete { error in

          if let error = error {

            print(error) 

          } else {

            self.present(self.loginController, animated: true, completion: nil)

        }

    }
 })
}

如果这仍然不起作用,请使用调试工具跟踪用户的生命周期....

关于ios - 删除用户后应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45598552/

相关文章:

ios - 旋转时更改 UIScreen 的大小

ios - UINavigationBar 在 Action Extension 中的旋转高度错误

swift - 有没有办法在快速 ViewController 中执行条件转场?

ios - 在 UITableViewController 上设置背景

ios - 从当前日期创建 NSDate 不正确

ios - 如何消除导航栏顶部的间隙?

ios - 我在字符串中收到格式不正确的 unicode

swift - 带圆角和背景颜色的 NSButton

java - Firebase 数据库空对象引用

java - 错误 : no suitable method found for setQuery(com. google.firebase.firestore.Query,Class<注意>)