I am trying to fetch all subcollections named jobs from different customer collections. I tried to get it with collectionGroup which I understand is you can fetch any collections from different parent. When I try like this list return empty.
class JobListViewModel : ObservableObject {
@Published var jobs : [Job]? = nil
func loadJobs() async throws {
Firestore.firestore().collectionGroup("jobs").getDocuments {
snapshot, error in
if let error = error {
print(error)
} else {
for document in snapshot!.documents {
self.jobs?.append(
Job(
id: "(document["id"])",
position: "(document["position"])",
salary: "(document["salary"])",
state: "(document["state"])",
city: "(document["city"])",
description: "(document["description"])"))
}
}
}
}
VStack {
List {
ForEach(viewmodel.jobs ?? [Job(id: "", position: "", salary: "", state: "", city: "", description: "")] ) { job in
Text(job.city)
Text(job.position)
}
}
}
.onAppear {
Task {
try await viewmodel.loadJobs()
}
}