Skip to content

Commit d92efb6

Browse files
committed
Handle moving element in FirebaseArray with a nil previousChildKey
1 parent 5583056 commit d92efb6

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

FirebaseDatabaseUI/FirebaseArray.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,11 @@ - (void)initListeners {
124124
[self.snapshots removeObjectAtIndex:fromIndex];
125125

126126
NSUInteger toIndex = 0;
127-
NSUInteger prevIndex = [self indexForKey:previousChildKey];
128-
if (prevIndex != NSNotFound) {
129-
toIndex = prevIndex + 1;
127+
if (previousChildKey != nil) {
128+
NSUInteger prevIndex = [self indexForKey:previousChildKey];
129+
if (prevIndex != NSNotFound) {
130+
toIndex = prevIndex + 1;
131+
}
130132
}
131133
[self.snapshots insertObject:snapshot atIndex:toIndex];
132134

FirebaseDatabaseUITests/FirebaseArrayTest.m

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,4 +410,36 @@ - (void)testFirebaseArrayCanMoveElementToStart {
410410
XCTAssert(expectedParametersWereCorrect, @"unexpected parameter in delegate callback");
411411
}
412412

413+
- (void)testFirebaseArrayMovesElementToStartWithNilPreviousKey {
414+
[self.observable populateWithCount:10];
415+
self.snap.key = @"6";
416+
417+
// Test delegate
418+
__block BOOL delegateWasCalled = NO;
419+
__block BOOL expectedParametersWereCorrect = NO;
420+
self.arrayDelegate.didMoveObject = ^(FirebaseArray *array, id object, NSUInteger from, NSUInteger to) {
421+
// Xcode complains about retain cycles if an XCTAssert is placed in here.
422+
delegateWasCalled = YES;
423+
expectedParametersWereCorrect = (array == self.firebaseArray &&
424+
object == self.snap &&
425+
from == 6 && to == 0);
426+
};
427+
428+
// Move 8 to the start
429+
[self.observable sendEvent:FIRDataEventTypeChildMoved withObject:self.snap previousKey:nil error:nil];
430+
431+
// Array expectation
432+
NSArray *items = self.firebaseArray.items;
433+
NSArray *expected = @[@"6", @"0", @"1", @"2", @"3", @"4", @"5", @"7", @"8", @"9"];
434+
NSMutableArray *result = [NSMutableArray array];
435+
for (FUIFakeSnapshot *snapshot in items) {
436+
[result addObject:snapshot.key];
437+
}
438+
XCTAssert([result isEqual:expected], @"expected firebaseArray contents to equal %@, got %@", expected, [result copy]);
439+
440+
// Delegate expectations
441+
XCTAssert(delegateWasCalled, @"expected delegate to receive callback for deletion");
442+
XCTAssert(expectedParametersWereCorrect, @"unexpected parameter in delegate callback");
443+
}
444+
413445
@end

0 commit comments

Comments
 (0)