Firestore 无法正确识别字段

Firestore not recognizing fields properly

所以我的目标是在加载 tableview 单元格的详细信息VC 时让已购买的事件显示某些按钮,而未购买的事件显示不同的按钮。所以我在这种情况下所做的是使用字段 purchased : true 对集合路径中的文档进行查询调用,如果有错误,请处理它,但如果没有t, 显示已购买活动的按钮。

    override func viewDidLoad() {
    super.viewDidLoad()
    
    
    viewPurchaseButton.isHidden = true
    cancelPurchaseButton.isHidden = true
   
    
    getSchoolDocumentID { (id) in
        if let ident = id {
            self.db.collection("school_users/\(ident)/events").whereField("purchased", isEqualTo: true).getDocuments { (querySnapshot, error) in
                if let error = error {
                    print("There was an error fetching the docs: \(error)")
                } else {
                    self.viewPurchaseButton.isHidden = false
                    self.cancelPurchaseButton.isHidden = false
                    self.purchaseTicketButton.isHidden = true
                    self.creditCard.isHidden = true
                }
            }
        }
        
    }

所以我运行认为它会起作用,我在集合中只有 4 个文档中有 1 个文档的字段 purchased 等于 true,所以我预计什么时候我在我的表 VC 中单击了该字段为 true 的单元格,详细信息 VC 将显示正确的按钮。详细信息 VC 显示了正确的按钮,但是当我离开 VC 并单击另一个 purchased 字段为 false 的单元格时,我很困惑为什么按钮出现了。如果有人能向我解释为什么会发生这种情况或指出我的代码存在的问题,那就太好了。

我相信您假设如果未找到匹配项,闭包中的代码将不会 运行,这是不正确的。

Firestore 查询闭包将始终运行闭包内的代码,无论是否有结果。

这是一个示例代码流

self.db.collection("school_users/\(ident)/events")... { querySnapshot, error
   // handle error

  // the below code always runs (unless there was an error handled above)   
  let docs = querySnapshot?.documents
  print(docs!.count) //will print 0 if no results, or the number of results
}

所以在你的问题中,只要没有错误,无论快照的内容如何,​​这段代码都会 运行s;

self.viewPurchaseButton.isHidden = false
self.cancelPurchaseButton.isHidden = false
self.purchaseTicketButton.isHidden = true
self.creditCard.isHidden = true

这样就可以了

self.db.collection("school_users/\(ident)/events").whereField("purchased", isEqualTo: true).getDocuments { (querySnapshot, error) in
    if let error = error {
        print("There was an error fetching the docs: \(error)")
        return
    }

    guard let docs = querySnapshot?.documents else { return }

    for doc in docs { //for testing to examine the returned docs, if any
       print(doc)
    }

    if docs.count > 0 {
        self.viewPurchaseButton.isHidden = false
        self.cancelPurchaseButton.isHidden = false
        self.purchaseTicketButton.isHidden = true
        self.creditCard.isHidden = true
    }
}

好的,我为此所做的工作是重构查询,而不是获取文档,只获取一个文档。然后我使用了这个功能,它就像美丽的一样! :)

override func viewDidLoad() {
    super.viewDidLoad()
    
    viewPurchaseButton.isHidden = true
    cancelPurchaseButton.isHidden = true
    
    getSchoolDocumentID { (schoolDocID) in
        if let schID = schoolDocID {
            self.db.document("school_users/\(schID)/events/\(self.selectedEventID!)").getDocument { (documentSnapshot, error) in
                if let error = error {
                    print("There was an error fetching the document: \(error)")
                } else {
                    
                    guard let docSnap = documentSnapshot!.get("purchased") else {
                        return
                    }
                    if docSnap as! Bool == true {
                        self.viewPurchaseButton.isHidden = false
                        self.cancelPurchaseButton.isHidden = false
                        self.creditCard.isHidden = true
                        self.purchaseTicketButton.isHidden = true
                        
                    } else {
                        self.creditCard.isHidden = false
                        self.purchaseTicketButton.isHidden = false
                    }
                }
            }
        }
    }

这样可以完美地完成工作。