Skip to content

Commit

Permalink
Ensure previous cells' prepareForReuse doesn't affect new cells by re…
Browse files Browse the repository at this point in the history
…moving the `onReuse` closure reference when releasing the cell for reuse.

Every time we update the table view state, existing cells can be reused for different index paths, where they'll be managed by different cell controllers - so we need to remove all references to the previous cell controllers when releasing the cell for reuse. Since the `onReuse` closure references the cell controller via `self`, we need to set `onReuse` to `nil` when releasing the cell for reuse by another cell controller. This way, we ensure there are no references to the cell controller so there will be no side effects.
  • Loading branch information
caiozullo committed Nov 2, 2023
1 parent d16329d commit 48be1be
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
17 changes: 17 additions & 0 deletions EssentialApp/EssentialAppTests/FeedUIIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,23 @@ class FeedUIIntegrationTests: XCTestCase {
XCTAssertEqual(view0.renderedImage, .none, "Expected no image state change for reused view once image loading completes successfully")
}

func test_feedImageView_showsDataForNewViewRequestAfterPreviousViewIsReused() throws {
let (sut, loader) = makeSUT()

sut.simulateAppearance()
loader.completeFeedLoading(with: [makeImage(), makeImage()])

let previousView = try XCTUnwrap(sut.simulateFeedImageViewNotVisible(at: 0))

let newView = try XCTUnwrap(sut.simulateFeedImageViewVisible(at: 0))
previousView.prepareForReuse()

let imageData = UIImage.make(withColor: .red).pngData()!
loader.completeImageLoading(with: imageData, at: 1)

XCTAssertEqual(newView.renderedImage, imageData)
}

func test_feedImageView_doesNotRenderLoadedImageWhenNotVisibleAnymore() {
let (sut, loader) = makeSUT()
sut.simulateAppearance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ extension FeedImageCellController: UITableViewDataSource, UITableViewDelegate, U
}

private func releaseCellForReuse() {
cell?.onReuse = nil
cell = nil
}
}
Expand Down

0 comments on commit 48be1be

Please sign in to comment.