Skip to content

Commit

Permalink
Skip reloading objects that are not found
Browse files Browse the repository at this point in the history
Summary:
Even with the assert, we will add `NSNotFound` to the set which will crash. Instead, skip adding the value in case a consumer is using a reference to an object that doesn't exist anymore.

Also, should be using `continue` if a section controller isn't returned by the data source.

Reviewed By: jessesquires

Differential Revision: D4226771

fbshipit-source-id: ed6df0992fdef611dd8fae64f4716e296df11f9a
  • Loading branch information
Ryan Nystrom authored and Facebook Github Bot committed Nov 24, 2016
1 parent 8a6fde9 commit ca15e29
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ This release closes the [2.0.0 milestone](https://github.com/Instagram/IGListKit

- Prevent `UICollectionView` bug when accessing a cell during working range updates. [Ryan Nystrom](https://github.com/rnystrom) [(#216)](https://github.com/Instagram/IGListKit/pull/216)

- Skip reloading for objects that are not found when calling `-[IGListAdapter reloadObjects:]`. [Ryan Nystrom](https://github.com/rnystrom) (tbd)

### Documentation

- We now have 100% documentation coverage. Docs been refined and clarified. [Jesse Squires](https://github.com/jessesquires) [(#207)](https://github.com/Instagram/IGListKit/pull/207)
Expand Down
9 changes: 6 additions & 3 deletions Source/IGListAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,11 @@ - (void)reloadObjects:(NSArray *)objects {

for (id object in objects) {
// look up the item using the map's lookup function. might not be the same item
NSUInteger section = [map sectionForObject:object];
IGAssert(section != NSNotFound, @"Did not find a section for item %@", object);
const NSInteger section = [map sectionForObject:object];
const BOOL notFound = section == NSNotFound;
if (notFound) {
continue;
}
[sections addIndex:section];

// reverse lookup the item using the section. if the pointer has changed the trigger update events and swap items
Expand Down Expand Up @@ -465,7 +468,7 @@ - (void)updateObjects:(NSArray *)objects {

IGAssert(sectionController != nil, @"Data source <%@> cannot return a nil section controller.", self.dataSource);
if (sectionController == nil) {
break;
continue;
}

// in case the section controller was created outside of -listAdapter:sectionControllerForObject:
Expand Down
7 changes: 7 additions & 0 deletions Tests/IGListAdapterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -843,4 +843,11 @@ - (void)test_whenDidEndDragging_thatScrollViewDelegateReceivesMethod {
[mockScrollDelegate verify];
}

- (void)test_whenReloadingObjectsThatDontExist_thatAdapterContinues {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
[self.adapter reloadObjects:@[@1, @3]];
XCTAssertEqual(self.collectionView.numberOfSections, 3);
}

@end

0 comments on commit ca15e29

Please sign in to comment.