diff --git a/Source/IGListAdapterUpdater.m b/Source/IGListAdapterUpdater.m index 75003eec..33b24548 100644 --- a/Source/IGListAdapterUpdater.m +++ b/Source/IGListAdapterUpdater.m @@ -280,12 +280,16 @@ void convertReloadToDeleteInsert(NSMutableIndexSet *reloads, NSMutableArray *itemMoves = batchUpdates.itemMoves; NSSet *uniqueDeletes = [NSSet setWithArray:itemDeletes]; + NSMutableSet *reloadDeletePaths = [NSMutableSet new]; + NSMutableSet *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 diff --git a/Tests/IGListAdapterE2ETests.m b/Tests/IGListAdapterE2ETests.m index 636779e6..3183bc84 100644 --- a/Tests/IGListAdapterE2ETests.m +++ b/Tests/IGListAdapterE2ETests.m @@ -1460,5 +1460,24 @@ [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 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