I am having a lot of trouble fetching data from my exerciseSet collection. It is a subset of my users collection. The exercise snapshot does not exist but it is finding the exercise document ID.
This is what the struct looks like:
struct fbExerciseSet: Identifiable, Codable { var id: String var reps: Int var weight: Int var time: Int var dateCompleted: Date var exercise: fbExercise } I have tested a modification where I got rid of the exercise component so I only returned the exerciseSet ID and that returned the 2 exerciseSets I have saved in firestore. So the issue seems to be with referencing the exercise subcollection and returning as fbExercise. I have tried so many things and I am really stuck. Any help is greatly appreciated. Here is my current function that is not returning any results in exerciseSetList:
func getExerciseSets2() { guard let currentUserID = Auth.auth().currentUser?.uid else { print("User is not logged in.") return } let db = Firestore.firestore() db.collection("users").document(currentUserID).collection("exerciseSet") .getDocuments { snapshot, error in if let error = error { print("Error fetching exercise sets:", error.localizedDescription) return } guard let snapshot = snapshot else { print("Snapshot is nil.") return } var exerciseSets: [fbExerciseSet] = [] let dispatchGroup = DispatchGroup() // Declare the dispatch group for document in snapshot.documents { let data = document.data() let reps = data["reps"] as? Int ?? 1 let weight = data["weight"] as? Int ?? 1 let time = data["time"] as? Int ?? 1 let dateCompleted = data["date"] as? Date ?? Date() if let exerciseRef = data["exercise"] as? DocumentReference { dispatchGroup.enter() exerciseRef.getDocument { exerciseSnapshot, exerciseError in defer { dispatchGroup.leave() } if let exerciseError = exerciseError { print("Error fetching exercise:", exerciseError.localizedDescription) return } guard let exerciseSnapshot = exerciseSnapshot, exerciseSnapshot.exists else { print("Exercise snapshot does not exist. Exercise ID:", exerciseRef.documentID) return } let exerciseData = exerciseSnapshot.data() let exercise = fbExercise( id: exerciseSnapshot.documentID, name: exerciseData?["name"] as? String ?? "", musclegroup: exerciseData?["musclegroup"] as? String ?? "", instructions: exerciseData?["instructions"] as? String ?? "" ) let exerciseSet = fbExerciseSet( id: document.documentID, reps: reps, weight: weight, time: time, dateCompleted: dateCompleted, exercise: exercise ) exerciseSets.append(exerciseSet) } } } dispatchGroup.notify(queue: .main) { // Update your exerciseSetList with the fetched exerciseSets self.exerciseSetList = exerciseSets print("COUNT: \(self.exerciseSetList.count)") } } } I get all these errors: Exercise snapshot does not exist. Exercise ID: GOkkx6b02NXG6XFVAtAI COUNT: 0 printed to console
It is finding the Exercise ID I am expecting but the exercise snapshot does not exist.
exerciseSetdocument, that document has a propertyexercisethat has a value starting with a path to/exercises(plural) but that path doesn't appear to exist.exercisepath that's referring to but the path looks incorrect.