0
private func loadUserLearntChants() {
    Task {
        do {
            try await profileViewModel.loadCurrentUser()
            if let userId = profileViewModel.user?.userId {
                let user = try await userManager.getUser(userId: userId)
                if let learntChants = user.learntChants {
                    learntChantIds = learntChants
                }
                print(user)
            }
        } catch {
            print("Error: \(error)")
        }
    }
}

When I print user it says learntChants is nil

Array of Objects (learntChants) is returning nil:

Array of Objects (learntChants) is returning nil

Screenshot from Firestore showing that learnt_chants isn't empty:

Screenshot from Firestore showing that learnt_chants isn't empty

I read Unpacking Firestore array with objects to a model in swift, which said that you need to cast it to key-value type (Dictionary) first, then map it to LearntChant object

I tried this but it still didn't return anything

private func loadUserLearntChants() {
    Task {
        do {
            try await profileViewModel.loadCurrentUser()
            if let userId = profileViewModel.user?.userId {
                let user = try await userManager.getUser(userId: userId)
                if let learntChants = user.learntChants as? [[String: Any]] {
                    let learntChantObjects = learntChants.map { learntChant in
                        let chant = LearntChant(chantId: learntChants["chantId"] as? String ?? "none", voteType: learntChants["voteType"] as? String ?? "none")
                        return chant
                    }
                }
            }
        } catch {
            print("Error: \(error)")
        }
    }
}
3
  • Where does an actor named profileViewModel come from? Commented Feb 19, 2024 at 5:20
  • Nothing is being done with learntChantObjects? In other words, the .map maps and creates a bunch of objects, and then adds them to let learntChantObjects but then `learntChantObjects' silently goes out of scope. Commented Feb 19, 2024 at 18:18
  • Also, the Firestore code is missing from the question so you should verify that you're actually reading objects from Firebase. There are a number of other places where the code could be failing so some additional troubleshooting would be a good idea (step through your code!). Please take a moment and review How to ask a good question and How to create a Minimal, Reproducible Example Commented Feb 19, 2024 at 18:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.