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)"
default:
self.errorMessage = "Error decoding document: \(error.localizedDescription)"
}
return nil
}
})))
} else {
self.errorMessage = "No documents in 'games' collection"
return
}
Any help is appreciated very much!!