Ios Firebase致命错误

时间:2017-11-10 03:26:01

标签: ios swift firebase

我运行我的程序,一旦模拟器打开就会崩溃。在崩溃之前打印之前的消息如下:

  

对于systemgroup.com.apple.configurationprofiles路径是   /Users/DrewGelinas/Library/Developer/CoreSimulator/Devices/97591C46-0149-4951-BDA6-FB0C063E396C/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles       (LLDB)

我正在运行的代码是:

override func viewDidLoad() {
        let ref = Database.database().reference()
        let myuser = Auth.auth().currentUser

        let userInfo = ref.child("Users").child((myuser?.uid)!)
        userInfo.observe(.value, with: {(snapshot) in
            //get the current users credentials
            self.userNo = snapshot.childSnapshot(forPath: "/PhoneNo").value as! String
        })
    }

错误发生在let userInfo =

1 个答案:

答案 0 :(得分:3)

您正在使用强制解包(uid)访问!,这会导致此次崩溃。

我的猜测是您目前没有用户登录Auth

用这样的东西重新删除爆炸:

        let ref = Database.database().reference()
        guard let myUserId = Auth.auth().currentUser?.uid else { return }

        let userInfo = ref.child("Users").child(myUserId)
        userInfo.observe(.value, with: {(snapshot) in
            //get the current users credentials
            self.userNo = snapshot.childSnapshot(forPath: "/PhoneNo").value as! String
        })

Guard语句意味着如果该值不存在,代码就会停在那里,因此您永远不会尝试观察Auth中不存在的用户。