Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mutating array datasource bug #1109

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The changelog for `IGListKit`. Also see the [releases](https://github.com/instag
- 5x improvement to diffing performance when result is only inserts or deletes. [Ryan Nystrom](https://github.com/rnystrom) [(tbd)](tbd)

### Fixes
- Copy objects when retrieving from datasource to prevent modification of models in binding section controller. [Kashish Goel](https://github.com/kashishgoel) [(#1109)](https://github.com/Instagram/IGListKit/pull/1109)

3.2.0
-----
Expand Down
2 changes: 1 addition & 1 deletion Source/IGListBindingSectionController.m
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ - (void)didUpdateToObject:(id)object {
self.object = object;

if (oldObject == nil) {
self.viewModels = [self.dataSource sectionController:self viewModelsForObject:object];
self.viewModels = [[self.dataSource sectionController:self viewModelsForObject:object] copy];
} else {
IGAssert([oldObject isEqualToDiffableObject:object],
@"Unequal objects %@ and %@ will cause IGListBindingSectionController to reload the entire section",
Expand Down
19 changes: 19 additions & 0 deletions Tests/IGListBindingSectionControllerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -296,5 +296,24 @@ - (void)test_whenUpdatingManually_with2Updates_thatBothCompletionBlocksCalled {
[self waitForExpectationsWithTimeout:30 handler:nil];
}

- (void)test_whenUpdating_withMutableArrayObject_thatViewModelsDontMutate {
NSArray *objects = @[
@"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];
[initObjects removeAllObjects];

XCTAssertEqual(oldModels, section.viewModels);
}

@end