我有一个用户集合,其中有一个名为 attendEvents 的子集合,如下图所示。如您所见,attendEvents 子集合中有 3 个文档
我尝试使用下面的代码获取该子集合上的所有可用文档,我只想使用 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)
}
}
}
但是如果我从模拟器中删除该应用程序并再次安装它,我就获得了所有三个数据。是被缓存还是什么?
最佳答案
我也遇到过同样的情况。就我而言,发生这种情况是因为在我的文档中它只包含子集合。这将导致文档本身不会显示在查询或快照中。 我的解决方法是在文档中添加一些随机信息以使其存在。
关于ios - 为什么我无法获取 firestore 子集合中存储的所有文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51807804/