Learn Courses My Dashboard

Firebase - how to define and store an array of maps

Hello together,

I would like to store in firebase an array of maps looking like this

my structs are defined as:

struct HalfInningResult: Codable {
    var runs: Int = 0
    var hits: Int? = nil
    var errors: Int? = nil
    var LOB: Int? = nil
}

struct InningResult: Codable {
    var home = HalfInningResult()
    var guest = HalfInningResult()
}

The function “updateScoreOfLiveGame” should store these values in firebase.

    func updateScoreOfLiveGame(game: Game, score: ScoreboardModel) {
                
        // declare vars for storing inningResults in firebase storage
        var homeInningResults = HalfInningResult()
        var guestInningResults = HalfInningResult()
        var inningResult = InningResult()
        var inningResults = [InningResult]()
        
        // decode inningResults for firebase storage
        // loop over all played innings
        for j in 0...(score.inningResults.count-1) {
            // append inningResults of each played inning
            homeInningResults.runs = score.inningResults[j]["home"]!.runs
            homeInningResults.hits = score.inningResults[j]["home"]?.hits
            homeInningResults.errors = score.inningResults[j]["home"]?.errors
            homeInningResults.LOB = score.inningResults[j]["home"]?.LOB
            inningResult.home = homeInningResults
            
            guestInningResults.runs = score.inningResults[j]["guest"]!.runs
            guestInningResults.hits = score.inningResults[j]["guest"]?.hits
            guestInningResults.errors = score.inningResults[j]["guest"]?.errors
            guestInningResults.LOB = score.inningResults[j]["guest"]?.LOB
            inningResult.guest = guestInningResults
            
            inningResults.append(inningResult)
        }
        
        // create dataToUpdate variable first
        let dataToUpdate: [String: Any] = [
            "city": game.city,
            "league": game.league,
            "ballpark": game.ballpark,
            "scheduledDate": game.scheduledDate,
            "startTime": score.startTime,
            "homeTeam": game.homeTeam,
            "homeTeamLogoUrl": String(game.homeTeamLogoUrl ?? ""),
            "homeTeamShort": game.homeTeamShort,
            "guestTeam": game.guestTeam,
            "guestTeamLogoUrl": String(game.guestTeamLogoUrl ?? ""),
            "guestTeamShort": game.guestTeamShort,
            "runs": [
                "home": Int(score.runs["home"] ?? 0),
                "guest": Int(score.runs["guest"] ?? 0)
            ],
            "score": [
                "balls": score.balls,
                "strikes": score.strikes,
                "outs": score.outs
                ],
            "inning": score.inning,
            "inningHalf": score.halfInning.rawValue,
            "inningResults": inningResults, 
            "bases": [
                "base1": bases.base1,
                "base2": bases.base2,
                "base3": bases.base3
            ]
        ]
        
        // Get reference to 'liveGames' collection within 'seasonleagues'
        let docRef = db.collection("liveGames").document("\(game.id!)")
        docRef.setData(dataToUpdate, merge: true) { error in
            if let error = error {
                print("Error while updating game data of gameID: \(game.id)")
                print(error.localizedDescription)
                // TODO: throw error
                //throw GameError.errorUpdateingLiveGameScore
            }
        }
    }

when calling the function “updateScoreOfLiveGame” I do get the following error message:

I understand, that “inningResults” is not a dictionary with a pair of values. But how do I have to declare “inningResults” so that these values can be stored in firebase? Or shall the whole “dataToUpdate” get translated into a JSON-format? How would you do this?

Any help appreciated.

Thx, Peter