alickbass / CodableFirebase

Use Codable with Firebase

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Decoding to Date not working?

zackshapiro opened this issue · comments

I'm saving a map in Firestore on the Bookmarks key which is a map of userIds to the following object

{
  "lastMessageDocumentId": String
  "messageCreatedAt": Timestamp (I save as a Date() but Firebase in the console shows Timestamp)
}

The dict (below) printed in my console

▿ 2 elements
  ▿ 0 : 2 elements
    - key : "lastMessageDocumentId"
    - value : tosinaf joined TTT
  ▿ 1 : 2 elements
    - key : "messageCreatedAt"
    - value : 2018-11-27 20:59:11 +0000

So below I'm trying to decode using the FirebaseDecoder and still getting nil

    final class Bookmarks {

        struct Bookmark: Decodable {
            let lastMessageDocumentId: String
            let messageCreatedAt: Date
        }

        init?(json: [String: [String: Any]]) {  
            for (_, dict) in json {
                let foo = try? FirebaseDecoder().decode(Bookmark.self, from: dict) // nil

Any advice here? Thanks!

Hi! Thanks for opening this issue! Could you please use FirestoreDecoder and FirestoreEncoder instead of FirebaseDecoder

Ah, that was the issue. Thanks @alickbass!