Swift 5 Firestore:检查数据库中是否存在集合

时间:2020-06-03 01:01:39

标签: swift firebase google-cloud-firestore nsexception

我正在尝试检查Firestore数据库中是否存在某个集合,这是这样做的代码:

let db = Firestore.firestore()
                db.collection(pickedClass).getDocuments { (snapshot, error) in

                    if error == nil && snapshot != nil {
                        if snapshot!.documents.count > 0 {

                        for document in snapshot!.documents {
                            let documentData = document.data()
                            let info = documentData["info"] as! [String: Any]

                            output.append(object)
                        }
                        self.performSegue(withIdentifier: "showTutors", sender: output)

                        } else {
                            self.createAlert(title: "No Tutors Found", message: "Sorry, there are no tutors for this class", buttonMsg: "Okay")
                        }
                }

            }

运行此命令得到的异常显然来自第二行db.collection(pickedClass).getDocuments,如下所示:

*** Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'Invalid collection reference. Collection references must have an odd number of segments, but  has 0'

terminating with uncaught exception of type NSException

奇怪的是,大多数情况下这不是问题,当集合不存在时用户会看到警报,但有时会发生这种情况。直到今天,我才从未遇到过这个问题,而且我已经运行了两个月以上的数据库和这段代码。

非常感谢您的帮助!

0 个答案:

没有答案