简体   繁体   English

如何从firebase获取数据?

[英]How do I fetch data from firebase?

I am new to swift and I am trying to do as best as I can not to bother others I have looked for days to solve a problem but still hopeless, I hope you guys could tell me how can I fix it. 我是新手,我正努力做到最好,因为我不能打扰别人我已经找了几天解决问题,但仍然没有希望,我希望你们能告诉我如何解决它。

I want to get data and to put it in newPerson enter code here newPersons.append(nyPerson!) and it crashes saying Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value 我想获取数据并将其放入newPerson这里输入代码newPersons.append(nyPerson!)并且它崩溃说Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional valuenewPersons.append(nyPerson!) Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Auth.auth().addStateDidChangeListener { (auth, user) in
            self.ref = Database.database().reference(withPath: "personer")
            self.ref.observe(.value, with: { (snapshot) in
                var newPersons:[BackUp] = []
                for item in snapshot.children {
                    let nyPerson = BackUp(snapshot: item as! DataSnapshot)
                    newPersons.append(nyPerson!)

                }
                self.personsArray = newPersons
                self.tableView.reloadData()
            })
        }

This is a code form Getting Started documentation: https://firebase.google.com/docs/database/ios/lists-of-data 这是代码表单“入门”文档: https//firebase.google.com/docs/database/ios/lists-of-data

I think the issue you have in BackUp.init(snapshot: DataSnapshot). 我认为你在BackUp.init(快照:DataSnapshot)中遇到的问题。 be sure to get data with as optional, for example: 一定要将数据作为可选项获取,例如:

init(snapshot: DataSnapshot) {
 let value = snapshot.value as? NSDictionary
 let username = value?["username"] as? String ?? ""
 ...
}

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

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