[英]Why am I getting a document reference error about segments in Swift?
I am displaying documents from my Firebase Firestore database into a UITableView
. 我正在将Firebase Firestore数据库中的文档显示到
UITableView
。 I have created an array called parties
that stores my custom data model Party
. 我创建了一个名为
parties
的数组,用于存储我的自定义数据模型Party
。 The data is displaying properly, however, whenever I select a cell, the app crashes and returns the error, "Invalid document reference. Document references must have an even number of segments, but hwh3cztnfcd5aC5ZRgyPNpFUsS33 has 1." 数据显示正常,但是,每当我选择一个单元格时,应用程序崩溃并返回错误“无效的文档引用。文档引用必须具有偶数个段,但hwh3cztnfcd5aC5ZRgyPNpFUsS33具有1.”
I tried deleting the didSelectRowAt
method but the error will still appear. 我尝试删除
didSelectRowAt
方法,但仍会出现错误。
This is the function I have created that loads the data from Firebase Firestore. 这是我创建的从Firebase Firestore加载数据的功能。
func loadAllParties() {
// Remove all data from the array parties.
parties.removeAll()
if Auth.auth().currentUser != nil {
if let user = Auth.auth().currentUser {
// The user's ID, unique to the Firebase project.
let uid = user.uid
let db = Firestore.firestore()
db.collection(uid).order(by: "timeStamp", descending: false).getDocuments() { (snapshot, error) in
if error != nil {
print("Unable to get documents: \(error)")
} else {
for document in (snapshot!.documents) {
let title = document.data()["title"] as? String ?? "New Party"
let location = document.data()["date"] as? String ?? "No Location"
let date = document.data()["date"] as? String ?? "No Date"
let startTime = document.data()["startTime"] as? String ?? "No Start Time"
let endTime = document.data()["endTime"] as? String ?? "No End Time"
let notes = document.data()["notes"] as? String ?? "No Notes"
self.parties.append(Party(title: title, location: location, date: date, startTime: startTime, endTime: endTime, notes: notes))
self.displayOnboardingIfNeccessary()
}
}
}
}
} else {
print("No user is currently signed in.")
}
}
This is the code for didSelectRowAt
. 这是
didSelectRowAt
的代码。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let party = yourPartiesTableView.indexPathForSelectedRow
let cell = tableView.cellForRow(at: party!) as! YourPartiesTableViewCell
selectedParty = cell.titleLabel.text
performSegue(withIdentifier: "showPartyViewController", sender: self)
}
My expectations were that when you tap the cell, it would perform a segue to the next view controller and let that view controller know what data to get from Firebase on that page. 我的期望是,当您点击单元格时,它会对下一个视图控制器执行一个segue,并让该视图控制器知道从该页面上的Firebase获取哪些数据。
What/why is this let party = yourPartiesTableView.indexPathForSelectedRow
? 什么/为什么这个
let party = yourPartiesTableView.indexPathForSelectedRow
?
You know the tapped row from indexPath.row so 你知道indexPath.row中的tapped行
let row = indexPath.row
Then get the party from your dataSource array , not your tableview. 然后从您的dataSource数组中获取聚会,而不是您的tableview。
let party = self.parties[row]
and get the title from that 并从中获得标题
let title = party.title
that being said... the other part of the question is 那就是说......问题的另一部分是
you tap the cell, it would perform a segue to the next view controller and let that view controller know what data to get from Firebase on that page.
您点击单元格,它将执行到下一个视图控制器的segue,并让该视图控制器知道从该页面上的Firebase获取的数据。
Here's a couple of options to make that happen. 以下是实现这一目标的几种选择。
If you know the party's .documentID (key), you can get that and pass it to the detail view controller, where then it would load the data from Firestore based on that key. 如果您知道该方的.documentID(密钥),则可以将其传递给详细视图控制器,然后根据该密钥从Firestore加载数据。
Second option is you pass the party object itself, and it would populate from that object. 第二个选项是您传递派对对象本身,它将从该对象填充。 The downside there is the data is not live updating.. ie if you pass the key, you can attach an observer to that Firestore document (node) so if any changes take place while the detail is being viewed, it will appear in the UI.
缺点是数据不是实时更新...即如果您传递密钥,您可以将观察者附加到该Firestore文档(节点),因此如果在查看详细信息时发生任何更改,它将显示在UI中。
There are about 162 other options as well, but those may be a good starting point. 还有大约162种其他选择,但这些可能是一个很好的起点。
Note: You may want to consider adding a .document_id property to your Party class so if something changes/need to edit/delete etc, you will know which document it is via that document_id (the . documentID ) 注意:您可能需要考虑增加一个.document_id属性的党课,所以如果有新的变化/需要编辑/删除等,你就会知道它是经由DOCUMENT_ID该文件(该documentID )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.