Learn Courses My Dashboard

Firebase: mapping codable struct doesn't work

Hy together,

I try to map documents fetched from firebase but it doesn’t work. Does anyone has an idea why?

please see parts of my code an the screenshot results:

struct Game: Identifiable, Codable {
    @DocumentID var id: String?             // -> UUID of firestore document
    var nr: Int?
    var status: String
    var league: String
    var homeTeam: String
    var homeTeamLogoUrl: String?
    var guestTeam: String
    var guestTeamLogoUrl: String?
    var scheduledDate: Date
    var ballpark: String
    var city: String
    enum CodingKeys: String, CodingKey {
        case id
        case nr
        case status
        case league
        case homeTeam
        case homeTeamLogoUrl
        case guestTeam
        case guestTeamLogoUrl
        case scheduledDate = "date"
        case ballpark
        case city


reading from firebase and trying to map …

var games = [Games]

let docRef = db.collection("countries").document("\(country)").collection("seasonleagues").document("\(temp[i].id!)").collection("games")
            let queryGames = try? await docRef.getDocuments() 
                if queryGames != nil {
games.append(contentsOf: (queryGames!.documents.compactMap({ (queryDocumentSnapshot) -> Game? in
                        let result = Result { try? queryDocumentSnapshot.data(as: Game.self) }

                        switch result {
                        case .success(let game):
                            if var game = game {
                                // A game value was successfully initialized from the DocumentSnapshot
                                //self.errorMessage = nil
                                // skip games without opponents (e.g. half finals, finals, ...)
                                if game.homeTeam != "" && game.guestTeam != "" {
                                    return game
                                } else {
                                    return nil
                            else {
                                // A nil value was initialized from the DocumentSnapshot,
                                // or the DocumentSnapshot was nil
                                self.errorMessage = "Game document doesn't exist."
                                return nil
                        case .failure(let error):
                            switch error {
                            case DecodingError.typeMismatch(_, let context):
                                self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
                            case DecodingError.valueNotFound(_, let context):
                                self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
                            case DecodingError.keyNotFound(_, let context):
                                self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
                            case DecodingError.dataCorrupted(let key):
                                self.errorMessage = "\(error.localizedDescription): \(key)"
                                self.errorMessage = "Error decoding document: \(error.localizedDescription)"
                            return nil
                } else {
                    self.errorMessage = "No documents in 'games' collection"

Any help is appreciated very much!!

Your coding keys are suggesting that your data source (Firebase) has a field named date which you are mapping to scheduledDate. In fact your data source field name is already scheduledDate so the Coding Keys are serving no purpose.

Try commenting out the enum CodingKeys altogether and see if that solves the issue.

1 Like

Thanks Chris. Your hint solved my problem.

Ah that’s good new Peter.

But another issued showed up, which I don’t understand - because it has been working before.

Do you have any idea why Xcode 14.3 shows this error message?

Sorry Peter, I have no idea. I have not used that kind of syntax so I am at a loss what to advise.

This means you are passing some kind of parameter to a function that doesn’t require one

Look at the function signature of the function you’re trying to write