我有一个用户集合,该用户集合的子集合称为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)
}
}
}
但是如果我从模拟器中删除该应用程序并重新安装,我将获得所有三个数据。是被缓存还是什么?
答案 0 :(得分:0)
我一直处于同样的情况。就我而言,这是因为在我的文档中它仅包含子集合。这将导致文档本身不会显示在查询或快照中。 我的解决方法是在文档中添加一些随机信息以使其存在。