diff --git a/CHANGELOG.md b/CHANGELOG.md index 4224b0a4d..438f2e5cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -94,6 +94,8 @@ This release closes the [2.0.0 milestone](https://github.com/Instagram/IGListKit - Skip reloading for objects that are not found when calling `-[IGListAdapter reloadObjects:]`. [Ryan Nystrom](https://github.com/rnystrom) (tbd) +- Fixes a crash when a reload is queued for an object that is deleted in the same runloop turn. [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/IGListAdapterUpdater.m b/Source/IGListAdapterUpdater.m index 06316d23a..b1b24634a 100644 --- a/Source/IGListAdapterUpdater.m +++ b/Source/IGListAdapterUpdater.m @@ -234,8 +234,16 @@ void convertReloadToDeleteInsert(NSMutableIndexSet *reloads, const NSUInteger from = hasObjects ? [result oldIndexForIdentifier:diffIdentifier] : idx; const NSUInteger to = hasObjects ? [result newIndexForIdentifier:diffIdentifier] : idx; [reloads removeIndex:from]; - [deletes addIndex:from]; - [inserts addIndex:to]; + + // if a reload is queued outside the diff and the object was inserted or deleted it cannot be + if (from != NSNotFound && to != NSNotFound) { + [deletes addIndex:from]; + [inserts addIndex:to]; + } else { + IGAssert([result.deletes containsIndex:idx], + @"Reloaded section %zi was not found in deletes with from: %zi, to: %zi, deletes: %@", + idx, from, to, deletes); + } }]; } diff --git a/Tests/IGListAdapterUpdaterTests.m b/Tests/IGListAdapterUpdaterTests.m index 82d2f36dc..8d2b08d78 100644 --- a/Tests/IGListAdapterUpdaterTests.m +++ b/Tests/IGListAdapterUpdaterTests.m @@ -384,4 +384,17 @@ - (void)test_whenConvertingReloads_withChanges_thatIndexMoves { XCTAssertTrue([inserts containsIndex:0]); } +- (void)test_whenReloadingSection_whenSectionRemoved_thatConvertMethodCorrects { + NSArray *from = @[@"a", @"b", @"c"]; + NSArray *to = @[@"a", @"c"]; + IGListIndexSetResult *result = IGListDiff(from, to, IGListDiffEquality); + NSMutableIndexSet *reloads = [NSMutableIndexSet indexSetWithIndex:1]; + NSMutableIndexSet *deletes = [NSMutableIndexSet new]; + NSMutableIndexSet *inserts = [NSMutableIndexSet new]; + convertReloadToDeleteInsert(reloads, deletes, inserts, result, from); + XCTAssertEqual(reloads.count, 0); + XCTAssertEqual(deletes.count, 0); + XCTAssertEqual(inserts.count, 0); +} + @end