Skip to content

Commit

Permalink
Merge pull request #421 from morganchen12/firestore-fix
Browse files Browse the repository at this point in the history
fix #420
  • Loading branch information
morganchen12 authored Mar 14, 2018
2 parents 6ba8f57 + 926c0d5 commit 5c7599c
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 20 deletions.
4 changes: 2 additions & 2 deletions FirebaseDatabaseUITests/FUIDatabaseTestUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ NS_ASSUME_NONNULL_BEGIN
@end

@interface FUIArrayTestDelegate : NSObject <FUICollectionDelegate>
@property (nonatomic, copy) void (^didStartUpdates)();
@property (nonatomic, copy) void (^didEndUpdates)();
@property (nonatomic, copy) void (^didStartUpdates)(void);
@property (nonatomic, copy) void (^didEndUpdates)(void);
@property (nonatomic, copy) void (^queryCancelled)(id<FUICollection> array, NSError *error);
@property (nonatomic, copy) void (^didAddObject)(id<FUICollection> array, id object, NSUInteger index);
@property (nonatomic, copy) void (^didChangeObject)(id<FUICollection> array, id object, NSUInteger index);
Expand Down
39 changes: 28 additions & 11 deletions FirebaseFirestoreUI/FUISnapshotArrayDiff.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,26 @@ @interface FUIUnorderedPair ()
@property (nonatomic, readwrite) id left;
@property (nonatomic, readwrite) id right;

- (instancetype)initWithLeft:(id)left right:(id)right NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;

@end

@implementation FUIUnorderedPair

- (instancetype)init {
abort();
}

- (instancetype)initWithLeft:(id)left right:(id)right {
self = [super init];
if (self != nil) {
_left = left;
_right = right;
}
return self;
}

- (NSUInteger)hash {
return [self.left hash] ^ [self.right hash];
}
Expand All @@ -127,13 +143,10 @@ - (id)copy {
@end

FUIUnorderedPair *FUIUnorderedPairMake(id left, id right) {
FUIUnorderedPair *pair = [[FUIUnorderedPair alloc] init];
pair.left = left;
pair.right = right;
FUIUnorderedPair *pair = [[FUIUnorderedPair alloc] initWithLeft:left right:right];
return pair;
}


@interface FUILCS ()
@property (nonatomic, readonly) NSMutableDictionary<FUIUnorderedPair<FUIArraySlice<id> *> *, NSMutableArray<id> *> *memo;
@end
Expand Down Expand Up @@ -365,17 +378,20 @@ - (instancetype)initWithInitialArray:(NSArray<FIRDocumentSnapshot *> *)initial
}

- (void)buildDiffsFromDocumentChanges:(NSArray<FIRDocumentChange *> *)documentChanges {
NSMutableDictionary<FIRDocumentSnapshot *, NSNumber *> *oldIndexes =
NSMutableDictionary<NSString *, NSNumber *> *oldIndexes =
[NSMutableDictionary dictionaryWithCapacity:_initial.count];
NSMutableDictionary<FIRDocumentSnapshot *, NSNumber *> *newIndexes =
NSMutableDictionary<NSString *, NSNumber *> *newIndexes =
[NSMutableDictionary dictionaryWithCapacity:_result.count];

NSArray<FIRDocumentSnapshot *> *initial = _initial;
NSArray<FIRDocumentSnapshot *> *result = _result;

// Ignore the FIRDocumentChange indexing, since we do our own
for (NSInteger i = 0; i < _initial.count; i++) {
oldIndexes[_initial[i]] = @(i);
oldIndexes[initial[i].documentID] = @(i);
}
for (NSInteger i = 0; i < _result.count; i++) {
newIndexes[_result[i]] = @(i);
newIndexes[result[i].documentID] = @(i);
}

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

for (FIRDocumentChange *change in documentChanges) {
FIRDocumentSnapshot *snapshot = change.document;
NSNumber *oldIndex = oldIndexes[snapshot];
NSNumber *newIndex = newIndexes[snapshot];
NSNumber *oldIndex = oldIndexes[snapshot.documentID];
NSNumber *newIndex = newIndexes[snapshot.documentID];
if (oldIndex == nil && newIndex == nil) { continue; }
switch (change.type) {
case FIRDocumentChangeTypeRemoved:
Expand Down Expand Up @@ -478,7 +494,8 @@ - (NSString *)description {
NSNumber *initial = _movedInitialIndexes[i];
NSNumber *final = _movedResultIndexes[i];
id object = _movedObjects[i];
[moved appendFormat:@" %li -> %li, %@\n", (long)initial.integerValue, final.integerValue, object];
[moved appendFormat:@" %li -> %li, %@\n",
(long)initial.integerValue, (long)final.integerValue, object];
}
[moved appendString:@")\n"];

Expand Down
8 changes: 8 additions & 0 deletions FirebaseFirestoreUITests/FUIDocumentChange.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@
+ (instancetype)changeWithType:(FIRDocumentChangeType)type document:(id)document;

@end

@interface FUIDocumentSnapshot: NSObject

@property (nonatomic, readwrite) NSString *documentID;

+ (instancetype)documentWithID:(NSString *)identifier;

@end
13 changes: 12 additions & 1 deletion FirebaseFirestoreUITests/FUIDocumentChange.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@

@implementation FUIDocumentChange

+ (instancetype)changeWithType:(FIRDocumentChangeType)type document:(id)document {
+ (instancetype)changeWithType:(FIRDocumentChangeType)type
document:(id)document {
FUIDocumentChange *change = [[FUIDocumentChange alloc] init];
change.type = type;
change.document = document;
return change;
}

@end

@implementation FUIDocumentSnapshot

+ (instancetype)documentWithID:(NSString *)identifier {
FUIDocumentSnapshot *doc = [[FUIDocumentSnapshot alloc] init];
doc.documentID = identifier;
return doc;
}

@end
55 changes: 49 additions & 6 deletions FirebaseFirestoreUITests/FUISnapshotArrayDiffTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,22 @@ - (void)testDiffGeneralCases {

- (void)testSimpleInsertionCase {
NSArray *initial = @[];
NSArray *result =
@[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n"];
NSArray *result = @[
[FUIDocumentSnapshot documentWithID:@"a"],
[FUIDocumentSnapshot documentWithID:@"b"],
[FUIDocumentSnapshot documentWithID:@"c"],
[FUIDocumentSnapshot documentWithID:@"d"],
[FUIDocumentSnapshot documentWithID:@"e"],
[FUIDocumentSnapshot documentWithID:@"f"],
[FUIDocumentSnapshot documentWithID:@"g"],
[FUIDocumentSnapshot documentWithID:@"h"],
[FUIDocumentSnapshot documentWithID:@"i"],
[FUIDocumentSnapshot documentWithID:@"j"],
[FUIDocumentSnapshot documentWithID:@"k"],
[FUIDocumentSnapshot documentWithID:@"l"],
[FUIDocumentSnapshot documentWithID:@"m"],
[FUIDocumentSnapshot documentWithID:@"n"],
];

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

- (void)testSimpleChangeCase {
NSArray *initial =
@[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n"];
NSArray *initial = @[
[FUIDocumentSnapshot documentWithID:@"a"],
[FUIDocumentSnapshot documentWithID:@"b"],
[FUIDocumentSnapshot documentWithID:@"c"],
[FUIDocumentSnapshot documentWithID:@"d"],
[FUIDocumentSnapshot documentWithID:@"e"],
[FUIDocumentSnapshot documentWithID:@"f"],
[FUIDocumentSnapshot documentWithID:@"g"],
[FUIDocumentSnapshot documentWithID:@"h"],
[FUIDocumentSnapshot documentWithID:@"i"],
[FUIDocumentSnapshot documentWithID:@"j"],
[FUIDocumentSnapshot documentWithID:@"k"],
[FUIDocumentSnapshot documentWithID:@"l"],
[FUIDocumentSnapshot documentWithID:@"m"],
[FUIDocumentSnapshot documentWithID:@"n"],
];

NSArray *result = initial;

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

- (void)testSimpleDeletionCase {
NSArray *initial =
@[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n"];
NSArray *initial = @[
[FUIDocumentSnapshot documentWithID:@"a"],
[FUIDocumentSnapshot documentWithID:@"b"],
[FUIDocumentSnapshot documentWithID:@"c"],
[FUIDocumentSnapshot documentWithID:@"d"],
[FUIDocumentSnapshot documentWithID:@"e"],
[FUIDocumentSnapshot documentWithID:@"f"],
[FUIDocumentSnapshot documentWithID:@"g"],
[FUIDocumentSnapshot documentWithID:@"h"],
[FUIDocumentSnapshot documentWithID:@"i"],
[FUIDocumentSnapshot documentWithID:@"j"],
[FUIDocumentSnapshot documentWithID:@"k"],
[FUIDocumentSnapshot documentWithID:@"l"],
[FUIDocumentSnapshot documentWithID:@"m"],
[FUIDocumentSnapshot documentWithID:@"n"],
];
NSArray *result = @[];

NSMutableArray *changes = [NSMutableArray arrayWithCapacity:14];
Expand Down

0 comments on commit 5c7599c

Please sign in to comment.