简体   繁体   English

为什么我在Swift中收到有关段的文档引用错误?

[英]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.

相关问题 我得到无效的文档参考。 文档引用必须在 xcode 中有偶数段错误 - Im getting Invalid document reference. Document references must have an even number of segments error in xcode 为什么在Swift操场上进行简单的加法操作时会出错? - Why I am getting error for simple addition operation in Swift playground? 为什么在 swift 中解析响应时出现错误 - Why am I getting an error when parsing response in swift 为什么在 Swift 中使用 AnyObject 时出现“valueForUndefinedKey”错误? - Why I am getting 'valueForUndefinedKey' error when using AnyObject in Swift? 我在Swift 4中遇到错误,如下所示: - I am getting an error in Swift 4 as follows : 我为什么会收到此错误:对成员'fetch(with:parse:completion :)的引用不明确 - Why am I getting this error: Ambiguous reference to member 'fetch(with:parse:completion:)' 为什么我在iOS上从Swift收到SSL错误调用服务? - Why am I getting an SSL error calling service from Swift on iOS? 为什么在命名 Swift 枚举大小写时出现“不是整数文字中的有效数字”错误? - Why am I getting 'is not a valid digit in integer literal' error while naming Swift enum case? 为什么我使用Swift Cocoa App和桥接头获得未声明类型的'PubNub'编译器错误? - Why am I getting an undeclared type 'PubNub' compiler error with Swift Cocoa App and bridging header? 谁能告诉我为什么我在执行此代码时遇到错误的身份验证错误(Swift)? - Can any one tell me why i am getting Bad authentication error while executing this code(Swift)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM