Firebase Firestore Swift, Timestamp but server time?

Published

With Firestore, I add a timestamp field like this

    var ref: DocumentReference? = nil
    ref = Firestore.firestore()
        .collection("something")
        .addDocument(data: [
            "name": name,
            "words": words,
            "created": Timestamp(date: Date())
        ]) { ...
            let theNewId = ref!.documentID
            ...
    }

That’s fine and works great, but it’s not really correct. Should be using the "server timestamp" which Firestore supplies.

Pls note this is on iOS (Swift) and Firestore, not Firebase.

What the heck is the syntax to get a server timestamp?

Source: Ios Questions

Published
Categorized as firebase, google-cloud-firestore, ios, swift Tagged , , ,

Answers

You can either assign your class attribute with @ServerTimestamp using the FirebaseFirestoreSwift and it will set the server timestamp when you add the document or you can create a CloudFunction to assign this field with the server timestamp in the onCreate lifecycle of your document, notice its imposible to get the server timestamp at runtime, since you’re not in the server.
Check here for more info about FirebaseFirestoreSwift: https://medium.com/firebase-developers/swiftui-mapping-firestore-documents-using-swift-codable-589b6bfe462d


Bruno Monteiro de Andrade

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq