Skip to content

[FireStore] How to decode Timestamp in Multiplatform Code (Android/iOS) #113

New issue

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

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

Already on GitHub? # to your account

Closed
wezley98 opened this issue Dec 9, 2020 · 8 comments
Closed
Labels
bug Something isn't working

Comments

@wezley98
Copy link

wezley98 commented Dec 9, 2020

Given

data class Article(
    val title: String = "",
    val subTitle: String = "",
    val image: String = "",
    val date: Double = 0.0,
    val content: String = ""
)

Firebase.firestore.collection("articles")
            .get()
            .documents
            .map { doc ->
                Article(
                    doc.get("title"),
                    doc.get("subTitle"),
                    doc.get("image"),
                    doc.get("date"),
                    doc.get("content")
                )
            }

When

date is type timestamp in Firestore.

Then

How do we decode this type in commonMain for example? I have tried setting Article.date to String and Double and both crash.

 kotlinx.serialization.SerializationException: Expected Timestamp(seconds=1606944088, nanoseconds=0) to be double

Does the SDK provide any method to help with decoding this automatically?

@nbransby
Copy link
Member

nbransby commented Dec 9, 2020

@wezley98
Copy link
Author

wezley98 commented Dec 9, 2020

@nbransby Thanks for the reply, I had already read the docs, I couldn't find anywhere to pass in the Class serializer for Article as .get() doesn't take any parameters the same way set() would. We are trying to read data from Firestore.

suspend fun get(): QuerySnapshot

@nbransby
Copy link
Member

nbransby commented Dec 9, 2020

@Serializable
data class Article(
    val title: String = "",
    val subTitle: String = "",
    val image: String = "",
    val date: Double = 0.0,
    val content: String = ""
)

Firebase.firestore.collection("articles")
            .get()
            .documents
            .map { doc -> doc.data(Article.serializer())  }

@nbransby
Copy link
Member

nbransby commented Dec 9, 2020

Or you might be affected by #64 ?

@wezley98
Copy link
Author

@nbransby Thanks for the code, I just tried that exact sample code in Android and still get,

kotlinx.serialization.SerializationException: Expected Timestamp(seconds=1606944088, nanoseconds=0) to be double

@nbransby nbransby added the bug Something isn't working label Dec 10, 2020
@matanmarciano
Copy link

@wezley98 any solution?

@RZahr
Copy link

RZahr commented May 11, 2022

What about encoding it ? i.e. adding a date in the where clause Query.where("updatedDate", greaterThan = what_to_place_here)

@manuelh19
Copy link

What about encoding it ? i.e. adding a date in the where clause Query.where("updatedDate", greaterThan = what_to_place_here)

Any updates on this?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants