alickbass / CodableFirebase

Use Codable with Firebase
MIT License
691 stars 91 forks source link

Could not cast value #30

Closed rayyanmaretan closed 6 years ago

rayyanmaretan commented 6 years ago

I got error when trying to decode using FirestoreDecodable.

Could not cast value of type 'Swift.Dictionary<Swift.String, Any>' to 'Project.StructModel'.

The debugger is pointing on Decoder.swift file, line 1234. else if options.skipFirestoreTypes && (T.self is FirestoreDecodable.Type) { decoded = value as! T }

How to resolve this?

alickbass commented 6 years ago

Hi! Thanks for reaching out! Can you give the code you use for decoding, please?

rayyanmaretan commented 6 years ago

Hi @alickbass Thank you for replying.

The code:


    func listen<T: FirestoreDecodable>(to collectionPath: String, result: @escaping ([T]?, Error?) -> ()) {
        firestore.collection(collectionPath).addSnapshotListener { (snapshot, error) in

            guard let documents = snapshot?.documents, !documents.isEmpty else { return }

            let models: [T] = documents.flatMap({
                try! FirestoreDecoder().decode(T.self, from: $0.data()!)
            })

            result(models, error)
        }
    }
alickbass commented 6 years ago

Hi! FirestoreDecodable is only used for GeoPoint, DocumentReference etc. Don’t use it directly, please use Codable instead.

rayyanmaretan commented 6 years ago

Thank you @alickbass