Skip to content

Commit 2ad5e1c

Browse files
authored
Fix b/119570746: Transaction get returns nil if document does not exist (#2236)
This makes the iOS behavior match Web and Android, returning a non-nil snapshot with exists=false, rather than a nil snapshot.
1 parent 020eb60 commit 2ad5e1c

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

Firestore/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# Unreleased
2+
- [changed] `Transaction.getDocument()` has been changed to return a non-nil
3+
`DocumentSnapshot` with `exists` equal to `false` if the document does not
4+
exist (instead of returning a nil DocumentSnapshot). Code that was previously
5+
doing `if (snapshot) { ... }` must be changed to
6+
`if (snapshot.exists) { ... }`.
27

38
# v0.16.1
49
- [fixed] Offline persistence now properly records schema downgrades. This is a

Firestore/Example/Tests/Integration/FSTTransactionTests.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ - (void)testGetNonexistentDocumentThenCreate {
8181
runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
8282
FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
8383
XCTAssertNil(*error);
84+
XCTAssertNotNil(snapshot);
8485
XCTAssertFalse(snapshot.exists);
8586
[transaction setData:@{@"foo" : @"bar"} forDocument:doc];
8687
return @YES;

Firestore/Source/API/FIRTransaction.mm

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,22 @@ - (void)getDocument:(FIRDocumentReference *)document
119119
HARD_ASSERT(documents.count == 1,
120120
"Mismatch in docs returned from document lookup.");
121121
FSTMaybeDocument *internalDoc = documents.firstObject;
122-
if ([internalDoc isKindOfClass:[FSTDocument class]]) {
122+
if ([internalDoc isKindOfClass:[FSTDeletedDocument class]]) {
123+
FIRDocumentSnapshot *doc =
124+
[FIRDocumentSnapshot snapshotWithFirestore:self.firestore
125+
documentKey:document.key
126+
document:nil
127+
fromCache:NO
128+
hasPendingWrites:NO];
129+
completion(doc, nil);
130+
} else if ([internalDoc isKindOfClass:[FSTDocument class]]) {
123131
FIRDocumentSnapshot *doc =
124132
[FIRDocumentSnapshot snapshotWithFirestore:self.firestore
125133
documentKey:internalDoc.key
126134
document:(FSTDocument *)internalDoc
127135
fromCache:NO
128136
hasPendingWrites:NO];
129137
completion(doc, nil);
130-
} else if ([internalDoc isKindOfClass:[FSTDeletedDocument class]]) {
131-
completion(nil, nil);
132138
} else {
133139
HARD_FAIL("BatchGetDocumentsRequest returned unexpected document type: %s",
134140
NSStringFromClass([internalDoc class]));

0 commit comments

Comments
 (0)