Skip to content

Commit 5c67653

Browse files
authored
Change timestampsInSnapshots default to true and deprecate the setting. (#2251)
1 parent 4e5c6d0 commit 5c67653

File tree

8 files changed

+34
-45
lines changed

8 files changed

+34
-45
lines changed

Firestore/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Unreleased
2+
- [changed] The `areTimestampsInSnapshotsEnabled` setting is now enabled by
3+
default so timestamp fields read from a FIRDocumentSnapshot will be returned
4+
as FIRTimestamp objects instead of NSDate. Any code expecting to receive an
5+
NSDate object must be updated.
26
- [changed] `Transaction.getDocument()` has been changed to return a non-nil
37
`DocumentSnapshot` with `exists` equal to `false` if the document does not
48
exist (instead of returning a nil DocumentSnapshot). Code that was previously

Firestore/Example/Tests/Integration/API/FIRFieldsTests.mm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,6 @@ - (FIRDocumentSnapshot *)snapshotWithTimestamps:(FIRTimestamp *)timestamp {
233233
return [self readDocumentForRef:doc];
234234
}
235235

236-
// Note: timestampsInSnapshotsEnabled is set to "true" in FSTIntegrationTestCase, so this test is
237-
// not affected by the current default in FIRFirestoreSettings.
238236
- (void)testTimestampsInSnapshots {
239237
FIRTimestamp *originalTimestamp = [FIRTimestamp timestampWithSeconds:100 nanoseconds:123456789];
240238
FIRDocumentReference *doc = [self documentRef];
@@ -268,7 +266,10 @@ - (void)setUp {
268266
[super setUp];
269267
// Settings can only be redefined before client is initialized, so this has to happen in setUp.
270268
FIRFirestoreSettings *settings = self.db.settings;
269+
#pragma clang diagnostic push
270+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
271271
settings.timestampsInSnapshotsEnabled = NO;
272+
#pragma clang diagnostic pop
272273
self.db.settings = settings;
273274
}
274275

Firestore/Example/Tests/Util/FSTIntegrationTestCase.mm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ - (FIRFirestore *)firestore {
115115
+ (void)setUpDefaults {
116116
defaultSettings = [[FIRFirestoreSettings alloc] init];
117117
defaultSettings.persistenceEnabled = YES;
118-
defaultSettings.timestampsInSnapshotsEnabled = YES;
119118

120119
// Check for a MobileHarness configuration, running against nightly or prod, which have live
121120
// SSL certs.

Firestore/Source/API/FIRDocumentSnapshot.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,12 @@ - (FSTFieldValueOptions *)optionsForServerTimestampBehavior:
200200
(FIRServerTimestampBehavior)serverTimestampBehavior {
201201
FSTServerTimestampBehavior internalBehavior =
202202
InternalServerTimestampBehavor(serverTimestampBehavior);
203+
#pragma clang diagnostic push
204+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
203205
return [[FSTFieldValueOptions alloc]
204206
initWithServerTimestampBehavior:internalBehavior
205207
timestampsInSnapshotsEnabled:self.firestore.settings.timestampsInSnapshotsEnabled];
208+
#pragma clang diagnostic pop
206209
}
207210

208211
- (nullable id)objectForKeyedSubscript:(id)key {

Firestore/Source/API/FIRFirestore.mm

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -227,34 +227,6 @@ - (void)ensureClientConfigured {
227227
HARD_ASSERT(_settings.host, "FirestoreSettings.host cannot be nil.");
228228
HARD_ASSERT(_settings.dispatchQueue, "FirestoreSettings.dispatchQueue cannot be nil.");
229229

230-
if (!_settings.timestampsInSnapshotsEnabled) {
231-
LOG_WARN(
232-
"The behavior for system Date objects stored in Firestore is going to change "
233-
"AND YOUR APP MAY BREAK.\n"
234-
"To hide this warning and ensure your app does not break, you need to add "
235-
"the following code to your app before calling any other Cloud Firestore methods:\n"
236-
"\n"
237-
"let db = Firestore.firestore()\n"
238-
"let settings = db.settings\n"
239-
"settings.areTimestampsInSnapshotsEnabled = true\n"
240-
"db.settings = settings\n"
241-
"\n"
242-
"With this change, timestamps stored in Cloud Firestore will be read back as "
243-
"Firebase Timestamp objects instead of as system Date objects. So you will "
244-
"also need to update code expecting a Date to instead expect a Timestamp. "
245-
"For example:\n"
246-
"\n"
247-
"// old:\n"
248-
"let date: Date = documentSnapshot.get(\"created_at\") as! Date\n"
249-
"// new:\n"
250-
"let timestamp: Timestamp = documentSnapshot.get(\"created_at\") as! Timestamp\n"
251-
"let date: Date = timestamp.dateValue()\n"
252-
"\n"
253-
"Please audit all existing usages of Date when you enable the new behavior. In a "
254-
"future release, the behavior will be changed to the new behavior, so if you do not "
255-
"follow these steps, YOUR APP MAY BREAK.");
256-
}
257-
258230
const DatabaseInfo database_info(*self.databaseID, util::MakeString(_persistenceKey),
259231
util::MakeString(_settings.host), _settings.sslEnabled);
260232

Firestore/Source/API/FIRFirestoreSettings.mm

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
static const BOOL kDefaultPersistenceEnabled = YES;
2626
static const int64_t kDefaultCacheSizeBytes = 100 * 1024 * 1024;
2727
static const int64_t kMinimumCacheSizeBytes = 1 * 1024 * 1024;
28-
// TODO(b/73820332): flip the default.
29-
static const BOOL kDefaultTimestampsInSnapshotsEnabled = NO;
28+
static const BOOL kDefaultTimestampsInSnapshotsEnabled = YES;
3029

3130
@implementation FIRFirestoreSettings
3231

@@ -54,7 +53,10 @@ - (BOOL)isEqual:(id)other {
5453
self.isSSLEnabled == otherSettings.isSSLEnabled &&
5554
self.dispatchQueue == otherSettings.dispatchQueue &&
5655
self.isPersistenceEnabled == otherSettings.isPersistenceEnabled &&
56+
#pragma clang diagnostic push
57+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
5758
self.timestampsInSnapshotsEnabled == otherSettings.timestampsInSnapshotsEnabled &&
59+
#pragma clang diagnostic pop
5860
self.cacheSizeBytes == otherSettings.cacheSizeBytes;
5961
}
6062

@@ -63,7 +65,10 @@ - (NSUInteger)hash {
6365
result = 31 * result + (self.isSSLEnabled ? 1231 : 1237);
6466
// Ignore the dispatchQueue to avoid having to deal with sizeof(dispatch_queue_t).
6567
result = 31 * result + (self.isPersistenceEnabled ? 1231 : 1237);
68+
#pragma clang diagnostic push
69+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
6670
result = 31 * result + (self.timestampsInSnapshotsEnabled ? 1231 : 1237);
71+
#pragma clang diagnostic pop
6772
result = 31 * result + (NSUInteger)self.cacheSizeBytes;
6873
return result;
6974
}
@@ -74,7 +79,10 @@ - (id)copyWithZone:(nullable NSZone *)zone {
7479
copy.sslEnabled = _sslEnabled;
7580
copy.dispatchQueue = _dispatchQueue;
7681
copy.persistenceEnabled = _persistenceEnabled;
82+
#pragma clang diagnostic push
83+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
7784
copy.timestampsInSnapshotsEnabled = _timestampsInSnapshotsEnabled;
85+
#pragma clang diagnostic pop
7886
copy.cacheSizeBytes = _cacheSizeBytes;
7987
return copy;
8088
}

Firestore/Source/Public/FIRFirestoreSettings.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,24 @@ NS_SWIFT_NAME(FirestoreSettings)
4848
@property(nonatomic, getter=isPersistenceEnabled) BOOL persistenceEnabled;
4949

5050
/**
51-
* Enables the use of FIRTimestamps for timestamp fields in FIRDocumentSnapshots.
51+
* Specifies whether to use FIRTimestamps for timestamp fields in FIRDocumentSnapshots. This is
52+
* now enabled by default and should not be disabled.
5253
*
53-
* Currently, Firestore returns timestamp fields as an NSDate but NSDate is implemented as a double
54-
* which loses precision and causes unexpected behavior when using a timestamp from a snapshot as
55-
* a part of a subsequent query.
54+
* Previously, Firestore returned timestamp fields as NSDate but NSDate is implemented as a double
55+
* which loses precision and causes unexpected behavior when using a timestamp from a snapshot as a
56+
* part of a subsequent query.
5657
*
57-
* Setting timestampsInSnapshotsEnabled to true will cause Firestore to return FIRTimestamp values
58-
* instead of NSDate, avoiding this kind of problem. To make this work you must also change any code
59-
* that uses NSDate to use FIRTimestamp instead.
58+
* So now Firestore returns FIRTimestamp values instead of NSDate, avoiding this kind of problem.
6059
*
61-
* NOTE: in the future timestampsInSnapshotsEnabled = true will become the default and this option
62-
* will be removed so you should change your code to use FIRTimestamp now and opt-in to this new
63-
* behavior as soon as you can.
60+
* To opt into the old behavior of returning NSDate objects, you can temporarily set
61+
* areTimestampsInSnapshotsEnabled to false.
62+
*
63+
* @deprecated This setting now defaults to true and will be removed in a future release. If you are
64+
* already setting it to true, just remove the setting. If you are setting it to false, you should
65+
* update your code to expect FIRTimestamp objects instead of NSDate and then remove the setting.
6466
*/
65-
@property(nonatomic, getter=areTimestampsInSnapshotsEnabled) BOOL timestampsInSnapshotsEnabled;
67+
@property(nonatomic, getter=areTimestampsInSnapshotsEnabled) BOOL timestampsInSnapshotsEnabled
68+
__attribute__((deprecated));
6669

6770
/**
6871
* Sets the cache size threshold above which the SDK will attempt to collect least-recently-used

Firestore/Swift/Tests/API/BasicCompileTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ func initializeDb() -> Firestore {
6363
let settings = FirestoreSettings()
6464
settings.host = "localhost"
6565
settings.isPersistenceEnabled = true
66-
settings.areTimestampsInSnapshotsEnabled = true
6766
settings.cacheSizeBytes = FirestoreCacheSizeUnlimited
6867
firestore.settings = settings
6968

0 commit comments

Comments
 (0)