firebase-ios-sdk icon indicating copy to clipboard operation
firebase-ios-sdk copied to clipboard

Saving Date.distantPast to Firestore leads to crash

Open algrid opened this issue 3 years ago • 2 comments

  • Xcode version: 13.4.1
  • Firebase SDK version: 8.10.0
  • Installation method: CocoaPods
  • Firebase Component: Firestore
  • Target platform(s): iOS

When I try to save to Firestore a date object that is Date.distantPast, I get the following crash:

*** Terminating app due to uncaught exception 'Invalid timestamp', reason: 'Timestamp seconds out of range: -62135769600'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff20405604 __exceptionPreprocess + 242
	1   libobjc.A.dylib                     0x00007fff201a4a45 objc_exception_throw + 48
	2   CoreFoundation                      0x00007fff204054e2 -[NSException initWithCoder:] + 0
	3   FirebaseFirestore                   0x000000010dba641d -[FIRTimestamp initWithSeconds:nanoseconds:] + 269
	4   FirebaseFirestore                   0x000000010dba6219 +[FIRTimestamp timestampWithDate:] + 201
	5   FirebaseFirestore                   0x000000010da5bf07 _ZN8firebase9firestore3api13MakeTimestampEP6NSDate + 55
	6   FirebaseFirestore                   0x000000010dbb6bc0 -[FSTUserDataReader parseScalarValue:context:] + 1616
	7   FirebaseFirestore                   0x000000010dbb43eb -[FSTUserDataReader parseData:context:] + 1275
	8   FirebaseFirestore                   0x000000010dbb4d4e __45-[FSTUserDataReader parseDictionary:context:]_block_invoke.65 + 254
	9   libswiftCore.dylib                  0x00007fff30c50631 $ss26_SwiftDeferredNSDictionaryC23enumerateKeysAndObjects7options5usingySi_ys9UnmanagedVyyXlG_AHSpys5UInt8VGtXBtFTf4dnn_n + 321
	10  libswiftCore.dylib                  0x00007fff30a316c5 $ss26_SwiftDeferredNSDictionaryC23enumerateKeysAndObjects7options5usingySi_ys9UnmanagedVyyXlG_AHSpys5UInt8VGtXBtFTo + 37
	11  FirebaseFirestore                   0x000000010dbb495b -[FSTUserDataReader parseDictionary:context:] + 1067
	12  FirebaseFirestore                   0x000000010dbb4079 -[FSTUserDataReader parseData:context:] + 393
	13  FirebaseFirestore                   0x000000010dbb25d5 -[FSTUserDataReader parsedMergeData:fieldMask:] + 405
	14  FirebaseFirestore                   0x000000010dafb5d3 -[FIRDocumentReference setData:merge:completion:] + 323
	15  FirebaseFirestore                   0x000000010dafb2c9 -[FIRDocumentReference setData:merge:] + 89

Looks like a bug, but if that's an intended behavior, is there a convenient way to check if a date object is ok for Firestore (or to convert it to the nearest valid one)?

algrid avatar Jul 12 '22 14:07 algrid

Firestore timestamp has to be Range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z, from https://firebase.google.com/docs/reference/swift/firebasefirestore/api/reference/Classes/Timestamp

We will consider adding a convenient method to check if a given time is acceptable.

wu-hui avatar Jul 12 '22 15:07 wu-hui

@wu-hui thanks!

algrid avatar Jul 12 '22 17:07 algrid