Skip to content

Commit e9ca822

Browse files
committed
Fix FIRDocumentChange returning incorrect values for not found
Fixes #3298.
1 parent 5cb2e9f commit e9ca822

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,35 @@ - (void)testQueriesFireFromCacheWhenOffline {
252252
[registration remove];
253253
}
254254

255+
- (void)testDocumentChangesUseNSNotFound {
256+
NSDictionary *testDocs = @{
257+
@"a" : @{@"foo" : @1},
258+
};
259+
FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
260+
261+
id<FIRListenerRegistration> registration =
262+
[collection addSnapshotListener:self.eventAccumulator.valueEventHandler];
263+
264+
FIRQuerySnapshot *querySnap = [self.eventAccumulator awaitEventWithName:@"initial event"];
265+
XCTAssertEqual(querySnap.documentChanges.count, 1);
266+
267+
FIRDocumentChange *change = querySnap.documentChanges[0];
268+
XCTAssertEqual(change.oldIndex, NSNotFound);
269+
XCTAssertEqual(change.newIndex, 0);
270+
271+
FIRDocumentReference *doc = change.document.reference;
272+
[self deleteDocumentRef:doc];
273+
274+
querySnap = [self.eventAccumulator awaitEventWithName:@"delete"];
275+
XCTAssertEqual(querySnap.documentChanges.count, 1);
276+
277+
change = querySnap.documentChanges[0];
278+
XCTAssertEqual(change.oldIndex, 0);
279+
XCTAssertEqual(change.newIndex, NSNotFound);
280+
281+
[registration remove];
282+
}
283+
255284
- (void)testCanHaveMultipleMutationsWhileOffline {
256285
FIRCollectionReference *col = [self collectionRef];
257286

Firestore/Source/API/FIRDocumentChange.mm

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@
2525

2626
NS_ASSUME_NONNULL_BEGIN
2727

28+
namespace {
29+
30+
/**
31+
* Converts from C++ document change indexes to Objective-C document change
32+
* indexes. Objective-C's NSNotFound is signed NSIntegerMax, not unsigned -1.
33+
*/
34+
constexpr NSUInteger MakeIndex(size_t index) {
35+
return index == DocumentChange::npos ? NSNotFound : index;
36+
}
37+
38+
} // namespace
39+
2840
@implementation FIRDocumentChange {
2941
DocumentChange _documentChange;
3042
}
@@ -66,11 +78,11 @@ - (FIRQueryDocumentSnapshot *)document {
6678
}
6779

6880
- (NSUInteger)oldIndex {
69-
return _documentChange.old_index();
81+
return MakeIndex(_documentChange.old_index());
7082
}
7183

7284
- (NSUInteger)newIndex {
73-
return _documentChange.new_index();
85+
return MakeIndex(_documentChange.new_index());
7486
}
7587

7688
@end

0 commit comments

Comments
 (0)