Skip to content

Commit

Permalink
Balance reload delete and insert calls
Browse files Browse the repository at this point in the history
Summary:
In Instagram@073fc07 we deduped delete calls, but since we convert reloads into delete+insert when only deduping deletes, we end up with unbalanced delete+insert calls.

Unit test added reproduced a crash we see internally. Fix passes the test.

Not adding a changelog entry since this is a new regression fixed between releases. #trivial

Issue fixed: t17539856

- [x] All tests pass. Demo project builds and runs.
- [x] I added tests, an experiment, or detailed why my change isn't tested.
Closes Instagram#687

Differential Revision: D4933545

Pulled By: rnystrom

fbshipit-source-id: d38a900a99b1aa796dd654ddedb42e3cb4ef4378
  • Loading branch information
Ryan Nystrom authored and facebook-github-bot committed Apr 22, 2017
1 parent 5fe27d8 commit ee4f3c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Source/IGListAdapterUpdater.m
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,16 @@ - (IGListBatchUpdateData *)flushCollectionView:(UICollectionView *)collectionVie
NSMutableArray<IGListMoveIndexPath *> *itemMoves = batchUpdates.itemMoves;

NSSet<NSIndexPath *> *uniqueDeletes = [NSSet setWithArray:itemDeletes];
NSMutableSet<NSIndexPath *> *reloadDeletePaths = [NSMutableSet new];
NSMutableSet<NSIndexPath *> *reloadInsertPaths = [NSMutableSet new];
for (IGListReloadIndexPath *reload in batchUpdates.itemReloads) {
if (![uniqueDeletes containsObject:reload.fromIndexPath]) {
[itemDeletes addObject:reload.fromIndexPath];
[itemInserts addObject:reload.toIndexPath];
[reloadDeletePaths addObject:reload.fromIndexPath];
[reloadInsertPaths addObject:reload.toIndexPath];
}
}
[itemDeletes addObjectsFromArray:[reloadDeletePaths allObjects]];
[itemInserts addObjectsFromArray:[reloadInsertPaths allObjects]];

IGListBatchUpdateData *updateData = [[IGListBatchUpdateData alloc] initWithInsertSections:inserts
deleteSections:deletes
Expand Down
19 changes: 19 additions & 0 deletions Tests/IGListAdapterE2ETests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1460,5 +1460,24 @@ - (void)FIXME_test_whenDeletingItemsTwice_withDataUpdatedTwice_thatAllUpdatesApp
[self waitForExpectationsWithTimeout:30 handler:nil];
}

- (void)test_whenReloadingSameItemTwice_thatDeletesAndInsertsAreBalanced {
[self setupWithObjects:@[
genTestObject(@1, @4),
]];

IGTestObject *object = self.dataSource.objects[0];
IGListSectionController *sectionController = [self.adapter sectionControllerForObject:object];

XCTestExpectation *expectation = genExpectation;
[sectionController.collectionContext performBatchAnimated:YES updates:^(id<IGListBatchContext> batchContext) {
[batchContext reloadInSectionController:sectionController atIndexes:[NSIndexSet indexSetWithIndex:0]];
[batchContext reloadInSectionController:sectionController atIndexes:[NSIndexSet indexSetWithIndex:0]];
} completion:^(BOOL finished2) {
XCTAssertEqual([self.collectionView numberOfSections], 1);
XCTAssertEqual([self.collectionView numberOfItemsInSection:0], 4);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:30 handler:nil];
}

@end

0 comments on commit ee4f3c9

Please sign in to comment.