Skip to content

Commit

Permalink
Fix crash when reloading section that gets deleted
Browse files Browse the repository at this point in the history
Summary: Rare, but very real crash that occurs when a reload is manually queued and then the section gets deleted in a diff.

Reviewed By: jeremycohen

Differential Revision: D4267678

fbshipit-source-id: a5bedf77934ff85f76830e3f0123a993fc5d885c
  • Loading branch information
Ryan Nystrom authored and Facebook Github Bot committed Dec 5, 2016
1 parent e1d6f52 commit 7c3d499
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions Source/IGListAdapterUpdater.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}];
}

Expand Down
13 changes: 13 additions & 0 deletions Tests/IGListAdapterUpdaterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7c3d499

Please sign in to comment.