ios - 为什么我无法获取 firestore 子集合中存储的所有文档?

标签 ios swift firebase google-cloud-firestore

我有一个用户集合,其中有一个名为 attendEvents 的子集合,如下图所示。如您所见,attendEvents 子集合中有 3 个文档

enter image description here

我尝试使用下面的代码获取该子集合上的所有可用文档,我只想使用 getDocuments 获取全部内容,没有顺序、限制或任何内容

    func getAttendedEventsFromBeginning(completion: @escaping (_ eventID: [String]?,QueryDocumentSnapshot?)->Void) {

        FirestoreDocumentReference.users(uidUser: uid).reference().collection("attendedEvents")
            .getDocuments { (snapshot, error) in

                let lastDocument = snapshot?.documents.last

                if let error = error {
                    completion(nil,lastDocument)
                    print("Error when fetching attended events documents in user subcollection: \(error.localizedDescription)")
                } else {
                    print("Successfully fetching attended events documents in user subcollection from Firestore ")

                    guard let documentsSnapshot = snapshot else {
                        completion(nil,lastDocument)
                        return
                    }

                    let eventDocuments = documentsSnapshot.documents
                    print("xxxxx")
                    print(eventDocuments)

                    var attendedeventIDs = [String]()

                    for document in eventDocuments {
                        let eventDictionary = document.data()
                        let theEvent = eventDictionary["eventID"] as! String
                        attendedeventIDs.append(theEvent)
                    }

                    print(attendedeventIDs)

                    completion(attendedeventIDs,lastDocument)
                }
            }
    }

但是结果我只得到了2个文档快照,应该是3个文档 enter image description here

但是如果我从模拟器中删除该应用程序并再次安装它,我就获得了所有三个数据。是被缓存还是什么?

最佳答案

我也遇到过同样的情况。就我而言,发生这种情况是因为在我的文档中它只包含子集合。这将导致文档本身不会显示在查询或快照中。 我的解决方法是在文档中添加一些随机信息以使其存在。

关于ios - 为什么我无法获取 firestore 子集合中存储的所有文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51807804/

相关文章:

swift - CS : equality of a promise in functional programming

firebase - 将 Firebase 电话身份验证 OTP 中的 CloudOTP 更改为随机数

javascript - Firebase 3 - javascript - 登录后获取facebook accessToken

ios - NSURLConnection 完成错误 - 1001

ios - 如何关闭 Storyboard中的导航 Controller ?

ios - 通过 InputAccessoryView 上的按钮更改 uidatepicker 的值

swift - 向下转型和可选 : is this code idiomatic?

android - Phonegap ErrorURL

ios - 是否可以在更大的 pdf 页面内绘制 pdf 矢量图像?

找不到 Firebase Crashlytics IOS SourcePackages