From b07e3b0728656fe30a301b779e65ca3734e236bd Mon Sep 17 00:00:00 2001 From: Kashish Goel Date: Tue, 20 Feb 2018 22:27:44 -0500 Subject: [PATCH 1/3] Fix mutating array datasource bug --- Source/IGListBindingSectionController.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/IGListBindingSectionController.m b/Source/IGListBindingSectionController.m index b7e732961..346ff00a2 100644 --- a/Source/IGListBindingSectionController.m +++ b/Source/IGListBindingSectionController.m @@ -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", From eb77852e9ce9cd647e6fa2aa90f4f06628f630a2 Mon Sep 17 00:00:00 2001 From: Kashish Goel Date: Wed, 21 Feb 2018 20:59:20 -0500 Subject: [PATCH 2/3] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f4113e4d..03c8efb06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ----- From 0e4c02a2c86c40a62e48463fded0b6a131aecdf0 Mon Sep 17 00:00:00 2001 From: Kashish Goel Date: Thu, 22 Feb 2018 00:04:43 -0500 Subject: [PATCH 3/3] Add test for bug fix --- Tests/IGListBindingSectionControllerTests.m | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Tests/IGListBindingSectionControllerTests.m b/Tests/IGListBindingSectionControllerTests.m index c5e204b72..3ac90cbd1 100644 --- a/Tests/IGListBindingSectionControllerTests.m +++ b/Tests/IGListBindingSectionControllerTests.m @@ -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