【发布时间】:2021-09-19 17:55:17
【问题描述】:
当我尝试从此视图获取文档时出现错误
线程 1:致命错误:在隐式展开以 db1.collection... 开头的行中的可选值时意外发现 nil。
但是如果我在另一个视图中尝试相同的获取文档,它会继续
这里有什么问题?
import UIKit
import FirebaseFirestore
class DetailPlantViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var detailTableView: UITableView!
var queryPlantData: String?
var db1:Firestore!
var plantDataArray = [PlantData]()
override func viewDidLoad() {
super.viewDidLoad()
detailTableView.delegate = self
detailTableView.dataSource = self
detailTableView.register(MyDetailTableViewCell.nib(), forCellReuseIdentifier: MyDetailTableViewCell.identifier)
tabBarController?.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Log Out", style: .plain, target: self, action: #selector(logOutPressed))
db1.collection("plants").getDocuments { (snapshot, error) in
if error == nil && snapshot != nil{
for document in snapshot!.documents{
let documentData = document.data()
print(documentData)
}
}
}
//loadPlantData()
}
【问题讨论】:
-
那行代码不会抛出该错误(假设 db1 已填充)- 可能在闭包中。但是,如果您计划在此时使用返回的 Firebase 数据,则此
//loadPlantData()可能是一个问题。由于 Firebase 的异步特性,firebase 闭包之后的任何内容都将在闭包中的代码之前执行。您能否添加一个断点并逐行遍历您的代码,直到您收到该错误并使用更多详细信息更新问题?
标签: swift firebase google-cloud-firestore