Apologies
You must be signed in to watch this lesson.
Firestore Codable Model Objects
SwiftUI Firebase Chat

To make our lives a lot easier, we can decode our Firebase objects directly using the Codable protocol. This allows us to avoid manually setting up properties through strings and dictionaries, which is always very error-prone.

import Foundationimport FirebaseFirestoreSwiftstruct ChatMessage: Codable, Identifiable {    @DocumentID var id: String?    let fromId, toId, text: String    let timestamp: Date}
import Foundationimport FirebaseFirestoreSwiftstruct RecentMessage: Codable, Identifiable {    @DocumentID var id: String?    let text, email: String    let fromId, toId: String    let profileImageUrl: String    let timestamp: Date        var username: String {        email.components(separatedBy: "@").first ?? email    }        var timeAgo: String {        let formatter = RelativeDateTimeFormatter()        formatter.unitsStyle = .abbreviated        return formatter.localizedString(for: timestamp, relativeTo: Date())    }}

Comments (0)
HELP & SUPPORT