From ca15e29cf1dadc6c396fe8f14f16c27f6a38519c Mon Sep 17 00:00:00 2001 From: Ryan Nystrom Date: Wed, 23 Nov 2016 16:26:26 -0800 Subject: [PATCH] Skip reloading objects that are not found 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 --- CHANGELOG.md | 2 ++ Source/IGListAdapter.m | 9 ++++++--- Tests/IGListAdapterTests.m | 7 +++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a3adc55..d4efe97f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/Source/IGListAdapter.m b/Source/IGListAdapter.m index 0cdf3c56..21f0087c 100644 --- a/Source/IGListAdapter.m +++ b/Source/IGListAdapter.m @@ -311,8 +311,11 @@ 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 @@ -465,7 +468,7 @@ 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: diff --git a/Tests/IGListAdapterTests.m b/Tests/IGListAdapterTests.m index 6ad1c7cc..5cd4ae56 100644 --- a/Tests/IGListAdapterTests.m +++ b/Tests/IGListAdapterTests.m @@ -843,4 +843,11 @@ XCTAssertEqual(CGPointEqualToPoint(point, p), YES); \ [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