Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firestore: encoding & decoding strategies #86

Closed
wants to merge 1 commit into from
Closed

Firestore: encoding & decoding strategies #86

wants to merge 1 commit into from

Conversation

mshershnev
Copy link

For now you can use encoding & decoding strategies for Firestore.

This request fix issue #44

@mshershnev
Copy link
Author

mshershnev commented Feb 11, 2020

        let encoder = FirestoreEncoder()

        encoder.dateEncodingStrategy = .custom { date, encoder in
            var container = encoder.singleValueContainer()
            try container.encode(Timestamp.init(date: date))
        }

        let decoder = FirestoreDecoder()

        decoder.dateDecodingStrategy = .custom { decoder -> Date in
            let container = try decoder.singleValueContainer()
            let timestamp = try container.decode(Timestamp.self)

            return timestamp.dateValue()
        }

@Moriquendi
Copy link

👍

@otri
Copy link

otri commented Mar 12, 2020

@mshershnev love it!
But, I get a warning:
Instance method 'decode' requires that 'Timestamp' conform to 'Decodable'

Wondering how you managed to get Timestamp to be decodable?

EDIT: Found It!

Include the swift cocoapods extensions in your Podfile:
pod 'FirebaseFirestoreSwift'

And where you need to use it in yourfile.swift:
import Firebase import FirebaseFirestoreSwift import CodableFirebase

Original thread on this topic is here:
Add support for object serialization in Firestore

@otri
Copy link

otri commented Mar 12, 2020

Unfortunately, after a lot of effort to make CodableFirebase better, I realized that Google's finished the complete solution in:
pod 'FirebaseFirestoreSwift'

Use Firestore.Decoder() directly. This will decode your struct Date() types correctly.

@mshershnev
Copy link
Author

@otri try to add 4 extensions:

extension GeoPoint: GeoPointType {}
extension Timestamp: TimestampType {}
extension FieldValue: FieldValueType {}
extension DocumentReference: DocumentReferenceType {}

@yvetterowe
Copy link

Unfortunately, after a lot of effort to make CodableFirebase better, I realized that Google's finished the complete solution in:
pod 'FirebaseFirestoreSwift'

Use Firestore.Decoder() directly. This will decode your struct Date() types correctly.

Thanks for the reference! Took me a decade to realize Google already supports this

@mshershnev mshershnev closed this by deleting the head repository Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants