0

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.

enter image description here enter image description here 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.

8
  • Does this answer your question? Thread 1: EXC_BAD_INSTRUCTION when fetching data Commented Aug 4, 2023 at 22:57
  • I may be overlooking something but the path shown in the screenshot is not valid. e.g. in the screenshot showing the exerciseSet document, that document has a property exercise that has a value starting with a path to /exercises (plural) but that path doesn't appear to exist. Commented Aug 5, 2023 at 17:19
  • @loremipsum no unfortunately, I have updated my question Commented Aug 7, 2023 at 18:54
  • @Jay thank you for catching that. I fixed that and am still having an issue which I updated the question to reflect. Thank you! Commented Aug 7, 2023 at 18:54
  • There's some ambiguity which makes it hard to follow the troubleshooting. First: exercise snapshot does not exist but it is finding the exercise document ID - how do you know it'd finding the documentID if the snapshot is empty? e.g. nil documents do not exist. Second: only returned the exerciseSet ID and that returned the 2 exerciseSets - the screenshot only shows one doc within exerciseSets, not 2 so how did it return 2? Lastly: Exercise snapshot does not exist - is that thrown from the error block? We don't know what exercise path that's referring to but the path looks incorrect. Commented Aug 7, 2023 at 19:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.