remove duplicate identifiers from IGListBindingSectionController objects

Summary:
I was building a new `IGListBindingSectionController` subclass and accidentally used two view models with the same ID. Was seeing strange results and realized we're not removing dups or asserting here.

Adding a call to `objectsWithDuplicateIdentifiersRemoved` when the view models are first requested.

Reviewed By: rnystrom

Differential Revision: D8303601

fbshipit-source-id: 42c62adc401feaec2c7dce2a83cfc6533599752b
This commit is contained in:
Adam Stern 2018-06-06 14:58:23 -07:00 committed by Facebook Github Bot
parent 0796e92a09
commit a1ee4c19f7
3 changed files with 27 additions and 8 deletions

View file

@ -16,6 +16,8 @@ The changelog for `IGListKit`. Also see the [releases](https://github.com/instag
- `[IGListAdapterUpdater performBatchUpdatesWithCollectionViewBlock:]` and `[IGListAdapterUpdater performReloadDataWithCollectionViewBlock:]` clean state and run completion blocks if their `UICollectionView` is nil. [Brandon Darin](https://github.com/jbd1030) (tbd)
- Ensuring view models with duplicate diff identifiers are removed when view models are first requested by `IGListBindingSectionController` [Adam Stern](https://github.com/adamastern) (tbd)
3.4.0
-----

View file

@ -112,7 +112,8 @@ typedef NS_ENUM(NSInteger, IGListDiffingSectionState) {
self.object = object;
if (oldObject == nil) {
self.viewModels = [[self.dataSource sectionController:self viewModelsForObject:object] copy];
NSArray *viewModels = [self.dataSource sectionController:self viewModelsForObject:object];
self.viewModels = objectsWithDuplicateIdentifiersRemoved(viewModels);
} else {
IGAssert([oldObject isEqualToDiffableObject:object],
@"Unequal objects %@ and %@ will cause IGListBindingSectionController to reload the entire section",

View file

@ -73,6 +73,19 @@
XCTAssertEqualObjects(cell12.textField.text, @"42");
}
- (void)test_withDuplicateDiffIdentifiers_thatDuplicatesAreRemoved {
[self setupWithObjects:@[
[[IGTestDiffingObject alloc] initWithKey:@1 objects:@[@7, @7]],
]];
XCTAssertEqual([self.collectionView numberOfSections], 1);
XCTAssertEqual([self.collectionView numberOfItemsInSection:0], 1);
IGTestNumberBindableCell *cell00 = [self cellAtSection:0 item:0];
XCTAssertEqualObjects(cell00.textField.text, @"7");
}
- (void)test_whenUpdating_withAddedModels_thatCellsCorrectAndConfigured {
[self setupWithObjects:@[
[[IGTestDiffingObject alloc] initWithKey:@1 objects:@[@7, @"seven"]],
@ -362,21 +375,24 @@
- (void)test_whenUpdating_withMutableArrayObject_thatViewModelsDontMutate {
NSArray *objects = @[
@"foo",
@"bar"
];
@"foo",
@"bar"
];
NSMutableArray *initObjects = [NSMutableArray arrayWithArray:objects];
[self setupWithObjects:@[
[[IGTestDiffingObject alloc] initWithKey:@1 objects:initObjects]
]];
IGTestDiffingSectionController *section = [self.adapter sectionControllerForObject:self.dataSource.objects.firstObject];
NSArray *oldModels = [section.viewModels copy];
XCTAssertNotEqual(initObjects, section.viewModels);
XCTAssertEqualObjects(initObjects, section.viewModels);
[initObjects removeAllObjects];
XCTAssertEqual(oldModels, section.viewModels);
XCTAssertNotEqualObjects(initObjects, section.viewModels);
}
@end