-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Round-tripped Firestore Timestamp fields have altered nanoseconds #14580
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
Comments
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight. |
Hi @natep, this is a limitation of Firestore's storage of date and time values:
Firestore is offered as part of the Firebase SDKs and uses Firebase's Timestamp class. This Timestamp class offers nanosecond precision, and is aligned with Google's proto timestamp format. Any value you write to Firestore, either the SDK cache or the Firestore backend, should have the additional precision rounded down. That should ease your concern about spurious change notifications. |
@MarkDuckworth thanks for taking a look at this. Part of my concern is that it makes it harder to for me to compare my own data structures that use Do other parts of the Firebase iOS SDK actually use the nanosecond precision? What would be ideal is: if the SDK can only be precise to microseconds, that But I get that this is a big SDK, and probably you can't undertake such a large change. |
A quick internal search indicated that Firebase Functions support nanosecond precision. I'm not certain about others. Your concern is noted, and this type of usability feedback is always valuable. However, I don't see this changing in the near term. I'm reaching out to the iOS team about a Timestamp initializer that could truncate to microseconds (cc: @ncooke3). One alternative option is to convert your Date instance to number of nanoseconds since epoch. I don't know how easy this is in Swift, but I'm assuming there is a solution. Firestore supports Int64, so this should support nanosecond precision until year ~2262. |
Description
Expected Result:
When round-tripping a
Timestamp
through Firestore, it is exactly the same before and after.Actual Result:
The nanosecond field of the
Timestamp
is consistently different.Why Is This A Problem:
It makes data consistency checking very difficult. It may result in spurious change notifications.
Reproducing the issue
This simple XCTest demonstrates the problem:
The assert will fail with an error like:
Note that my test framework is configured to hit a local Firebase emulator instead of the real infrastructure - I don't know if that makes a difference. I also tried specifying
cache
as the source forgetDocument(as:)
, but that didn't make a difference.This does not appear to be a problem with encoding and decoding, because this test works just fine:
Firebase SDK Version
11.7.0
Xcode Version
16.2
Installation Method
Swift Package Manager
Firebase Product(s)
Firestore
Targeted Platforms
iOS
Relevant Log Output
If using Swift Package Manager, the project's Package.resolved
Expand
Package.resolved
snippetIf using CocoaPods, the project's Podfile.lock
Expand
Podfile.lock
snippetReplace this line with the contents of your Podfile.lock!
The text was updated successfully, but these errors were encountered: