Skip to content

Commit 5c7599c

Browse files
authored
Merge pull request #421 from morganchen12/firestore-fix
fix #420
2 parents 6ba8f57 + 926c0d5 commit 5c7599c

File tree

5 files changed

+99
-20
lines changed

5 files changed

+99
-20
lines changed

FirebaseDatabaseUITests/FUIDatabaseTestUtils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ NS_ASSUME_NONNULL_BEGIN
9292
@end
9393

9494
@interface FUIArrayTestDelegate : NSObject <FUICollectionDelegate>
95-
@property (nonatomic, copy) void (^didStartUpdates)();
96-
@property (nonatomic, copy) void (^didEndUpdates)();
95+
@property (nonatomic, copy) void (^didStartUpdates)(void);
96+
@property (nonatomic, copy) void (^didEndUpdates)(void);
9797
@property (nonatomic, copy) void (^queryCancelled)(id<FUICollection> array, NSError *error);
9898
@property (nonatomic, copy) void (^didAddObject)(id<FUICollection> array, id object, NSUInteger index);
9999
@property (nonatomic, copy) void (^didChangeObject)(id<FUICollection> array, id object, NSUInteger index);

FirebaseFirestoreUI/FUISnapshotArrayDiff.m

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,26 @@ @interface FUIUnorderedPair ()
101101
@property (nonatomic, readwrite) id left;
102102
@property (nonatomic, readwrite) id right;
103103

104+
- (instancetype)initWithLeft:(id)left right:(id)right NS_DESIGNATED_INITIALIZER;
105+
- (instancetype)init NS_UNAVAILABLE;
106+
104107
@end
105108

106109
@implementation FUIUnorderedPair
107110

111+
- (instancetype)init {
112+
abort();
113+
}
114+
115+
- (instancetype)initWithLeft:(id)left right:(id)right {
116+
self = [super init];
117+
if (self != nil) {
118+
_left = left;
119+
_right = right;
120+
}
121+
return self;
122+
}
123+
108124
- (NSUInteger)hash {
109125
return [self.left hash] ^ [self.right hash];
110126
}
@@ -127,13 +143,10 @@ - (id)copy {
127143
@end
128144

129145
FUIUnorderedPair *FUIUnorderedPairMake(id left, id right) {
130-
FUIUnorderedPair *pair = [[FUIUnorderedPair alloc] init];
131-
pair.left = left;
132-
pair.right = right;
146+
FUIUnorderedPair *pair = [[FUIUnorderedPair alloc] initWithLeft:left right:right];
133147
return pair;
134148
}
135149

136-
137150
@interface FUILCS ()
138151
@property (nonatomic, readonly) NSMutableDictionary<FUIUnorderedPair<FUIArraySlice<id> *> *, NSMutableArray<id> *> *memo;
139152
@end
@@ -365,17 +378,20 @@ - (instancetype)initWithInitialArray:(NSArray<FIRDocumentSnapshot *> *)initial
365378
}
366379

367380
- (void)buildDiffsFromDocumentChanges:(NSArray<FIRDocumentChange *> *)documentChanges {
368-
NSMutableDictionary<FIRDocumentSnapshot *, NSNumber *> *oldIndexes =
381+
NSMutableDictionary<NSString *, NSNumber *> *oldIndexes =
369382
[NSMutableDictionary dictionaryWithCapacity:_initial.count];
370-
NSMutableDictionary<FIRDocumentSnapshot *, NSNumber *> *newIndexes =
383+
NSMutableDictionary<NSString *, NSNumber *> *newIndexes =
371384
[NSMutableDictionary dictionaryWithCapacity:_result.count];
372385

386+
NSArray<FIRDocumentSnapshot *> *initial = _initial;
387+
NSArray<FIRDocumentSnapshot *> *result = _result;
388+
373389
// Ignore the FIRDocumentChange indexing, since we do our own
374390
for (NSInteger i = 0; i < _initial.count; i++) {
375-
oldIndexes[_initial[i]] = @(i);
391+
oldIndexes[initial[i].documentID] = @(i);
376392
}
377393
for (NSInteger i = 0; i < _result.count; i++) {
378-
newIndexes[_result[i]] = @(i);
394+
newIndexes[result[i].documentID] = @(i);
379395
}
380396

381397
NSMutableArray<NSNumber *> *deletedIndexes = [NSMutableArray array];
@@ -395,8 +411,8 @@ - (void)buildDiffsFromDocumentChanges:(NSArray<FIRDocumentChange *> *)documentCh
395411

396412
for (FIRDocumentChange *change in documentChanges) {
397413
FIRDocumentSnapshot *snapshot = change.document;
398-
NSNumber *oldIndex = oldIndexes[snapshot];
399-
NSNumber *newIndex = newIndexes[snapshot];
414+
NSNumber *oldIndex = oldIndexes[snapshot.documentID];
415+
NSNumber *newIndex = newIndexes[snapshot.documentID];
400416
if (oldIndex == nil && newIndex == nil) { continue; }
401417
switch (change.type) {
402418
case FIRDocumentChangeTypeRemoved:
@@ -478,7 +494,8 @@ - (NSString *)description {
478494
NSNumber *initial = _movedInitialIndexes[i];
479495
NSNumber *final = _movedResultIndexes[i];
480496
id object = _movedObjects[i];
481-
[moved appendFormat:@" %li -> %li, %@\n", (long)initial.integerValue, final.integerValue, object];
497+
[moved appendFormat:@" %li -> %li, %@\n",
498+
(long)initial.integerValue, (long)final.integerValue, object];
482499
}
483500
[moved appendString:@")\n"];
484501

FirebaseFirestoreUITests/FUIDocumentChange.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@
2424
+ (instancetype)changeWithType:(FIRDocumentChangeType)type document:(id)document;
2525

2626
@end
27+
28+
@interface FUIDocumentSnapshot: NSObject
29+
30+
@property (nonatomic, readwrite) NSString *documentID;
31+
32+
+ (instancetype)documentWithID:(NSString *)identifier;
33+
34+
@end

FirebaseFirestoreUITests/FUIDocumentChange.m

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,22 @@
1818

1919
@implementation FUIDocumentChange
2020

21-
+ (instancetype)changeWithType:(FIRDocumentChangeType)type document:(id)document {
21+
+ (instancetype)changeWithType:(FIRDocumentChangeType)type
22+
document:(id)document {
2223
FUIDocumentChange *change = [[FUIDocumentChange alloc] init];
2324
change.type = type;
2425
change.document = document;
2526
return change;
2627
}
2728

2829
@end
30+
31+
@implementation FUIDocumentSnapshot
32+
33+
+ (instancetype)documentWithID:(NSString *)identifier {
34+
FUIDocumentSnapshot *doc = [[FUIDocumentSnapshot alloc] init];
35+
doc.documentID = identifier;
36+
return doc;
37+
}
38+
39+
@end

FirebaseFirestoreUITests/FUISnapshotArrayDiffTest.m

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,22 @@ - (void)testDiffGeneralCases {
262262

263263
- (void)testSimpleInsertionCase {
264264
NSArray *initial = @[];
265-
NSArray *result =
266-
@[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n"];
265+
NSArray *result = @[
266+
[FUIDocumentSnapshot documentWithID:@"a"],
267+
[FUIDocumentSnapshot documentWithID:@"b"],
268+
[FUIDocumentSnapshot documentWithID:@"c"],
269+
[FUIDocumentSnapshot documentWithID:@"d"],
270+
[FUIDocumentSnapshot documentWithID:@"e"],
271+
[FUIDocumentSnapshot documentWithID:@"f"],
272+
[FUIDocumentSnapshot documentWithID:@"g"],
273+
[FUIDocumentSnapshot documentWithID:@"h"],
274+
[FUIDocumentSnapshot documentWithID:@"i"],
275+
[FUIDocumentSnapshot documentWithID:@"j"],
276+
[FUIDocumentSnapshot documentWithID:@"k"],
277+
[FUIDocumentSnapshot documentWithID:@"l"],
278+
[FUIDocumentSnapshot documentWithID:@"m"],
279+
[FUIDocumentSnapshot documentWithID:@"n"],
280+
];
267281

268282
NSMutableArray *changes = [NSMutableArray arrayWithCapacity:14];
269283
for (NSInteger i = 0; i < 14; i++) {
@@ -295,8 +309,23 @@ - (void)testSimpleInsertionCase {
295309
}
296310

297311
- (void)testSimpleChangeCase {
298-
NSArray *initial =
299-
@[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n"];
312+
NSArray *initial = @[
313+
[FUIDocumentSnapshot documentWithID:@"a"],
314+
[FUIDocumentSnapshot documentWithID:@"b"],
315+
[FUIDocumentSnapshot documentWithID:@"c"],
316+
[FUIDocumentSnapshot documentWithID:@"d"],
317+
[FUIDocumentSnapshot documentWithID:@"e"],
318+
[FUIDocumentSnapshot documentWithID:@"f"],
319+
[FUIDocumentSnapshot documentWithID:@"g"],
320+
[FUIDocumentSnapshot documentWithID:@"h"],
321+
[FUIDocumentSnapshot documentWithID:@"i"],
322+
[FUIDocumentSnapshot documentWithID:@"j"],
323+
[FUIDocumentSnapshot documentWithID:@"k"],
324+
[FUIDocumentSnapshot documentWithID:@"l"],
325+
[FUIDocumentSnapshot documentWithID:@"m"],
326+
[FUIDocumentSnapshot documentWithID:@"n"],
327+
];
328+
300329
NSArray *result = initial;
301330

302331
NSMutableArray *changes = [NSMutableArray arrayWithCapacity:14];
@@ -329,8 +358,22 @@ - (void)testSimpleChangeCase {
329358
}
330359

331360
- (void)testSimpleDeletionCase {
332-
NSArray *initial =
333-
@[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n"];
361+
NSArray *initial = @[
362+
[FUIDocumentSnapshot documentWithID:@"a"],
363+
[FUIDocumentSnapshot documentWithID:@"b"],
364+
[FUIDocumentSnapshot documentWithID:@"c"],
365+
[FUIDocumentSnapshot documentWithID:@"d"],
366+
[FUIDocumentSnapshot documentWithID:@"e"],
367+
[FUIDocumentSnapshot documentWithID:@"f"],
368+
[FUIDocumentSnapshot documentWithID:@"g"],
369+
[FUIDocumentSnapshot documentWithID:@"h"],
370+
[FUIDocumentSnapshot documentWithID:@"i"],
371+
[FUIDocumentSnapshot documentWithID:@"j"],
372+
[FUIDocumentSnapshot documentWithID:@"k"],
373+
[FUIDocumentSnapshot documentWithID:@"l"],
374+
[FUIDocumentSnapshot documentWithID:@"m"],
375+
[FUIDocumentSnapshot documentWithID:@"n"],
376+
];
334377
NSArray *result = @[];
335378

336379
NSMutableArray *changes = [NSMutableArray arrayWithCapacity:14];

0 commit comments

Comments
 (0)