Skip to content

fix(ios): never re-attach torn-down ListView item views - #11642

Merged
NathanWalker merged 4 commits into
mainfrom
fix/ios-list-view-cell-teardown
Nov 14, 2026
Merged

NathanWalker merged 4 commits into
mainfrom
fix/ios-list-view-cell-teardown

Conversation

@edusperoni

Copy link
Copy Markdown
Contributor

PR Checklist

What is the current behavior?

On iOS, when a cell leaves the table (willMoveToSuperview(nil)), ListView tears its item view down (_removeContainer), but two things are left behind:

  • The torn-down view's dead native root stays in cell.contentView. ListView is not a CustomLayoutView, so removing a child doesn't detach its native view.
  • The cell's owner WeakRef keeps pointing at the torn-down view.

If UIKit later hands that cell back, _prepareCell re-attaches the torn-down view. Once that view left _map, nothing held its graph strongly, so native objects it caches may already have been released. One example is the UIColor cached by a CSS Color. Re-applying such a color throws inside tableView:cellForRowAtIndexPath:, the data source returns nil, and UIKit aborts:

NSInternalInconsistencyException: UITableView dataSource returned a nil cell for row at index path: {0, 19}

UIKit can also put a cell it removed back into the table without calling tableView:cellForRowAtIndexPath: again. That row stays blank.

Plain scrolling doesn't hit any of this. While recycling, UIKit hides cells rather than removing them: scrolling 300 rows back and forth tore down 0 cells and used 27 in total. Under an accessibility client (XCUITest, and likely VoiceOver), UIKit effectively stops reusing cells and removes and re-adds them all the time. In one XCUITest journey a single list built 1,203 cells for 36 rows. A shop-style benchmark app with paged ListViews, driven by XCUITest, crashed within 70 seconds in both runs.

What is the new behavior?

  • Teardown detaches the item's native root from the cell and clears the cell's reference to it. The next prepare builds a new item view instead of reviving the torn-down one. Sticky header views get the same teardown.
  • A cell that UIKit puts back into the table without content is prepared again. This happens in didMoveToSuperview, and in tableView:willDisplayCell:forRowAtIndexPath: for cells that are in the table.
  • Cells announced in willDisplay after they were taken out of the table are left alone. UIKit drops them, and preparing them would keep them and their item views in the list's map for good.
  • The prepare-and-arrange code from tableView:cellForRowAtIndexPath: moves to _prepareCellForDisplay, so both paths share it.

Teardown still happens as soon as a cell leaves the table. Keeping item views attached instead would pin one per cell, and with reuse disabled that reaches GB-scale memory.

Validation

  • Core automated iOS suite: 1,836 passed, 0 failed (iOS 26.3 simulator).

  • New test: test_cell_displayed_again_after_teardown_gets_new_content_ios removes a displayed cell from the table, sends willDisplay while it is out, then puts it back. It fails on main.

  • Benchmark app, XCUITest leak loop (paged ListViews, about 16 min per run) with its own workaround for this bug removed:

    Runtime main This PR
    @nativescript/ios 9.1.1 crashed in 2 of 2 runs, both within 70 s 2 of 2 runs passed, no blank rows
  • Live wrapper counts at the end of the loop: the same as on main (3 ListViewCell, 29 UIImage), so nothing accumulates. Live counts come from a development runtime build that can report them.

  • Memory on an iPhone 11 Pro, over the app's standard XCUITest journey, median [min–max] in MB:

    Runtime Core Peak After journey
    @nativescript/ios 9.1.1 this PR (3 runs) 144.1 [142.7–144.5] 122.0 [121.5–122.2]
    development build, default lifetime mode main (3 runs) 144.5 [144.0–145.9] 124.3 [123.7–125.0]
    development build, default lifetime mode this PR (2 runs) 143.5 [141.8–145.2] 123.9 [121.3–126.6]

    main was measured on a development runtime build because that build reports a released native object instead of crashing. Memory is unchanged: item views still go away as soon as their cell leaves the table.

Follow-ups (not in this PR)

  • Headers: they have no re-prepare hook. If UIKit re-adds a header view without asking viewForHeaderInSection, it stays blank, as it does today.
  • Rebuilding a view for every re-display: when an accessibility client disables reuse, each re-display builds a new item view. Avoiding that safely needs a reliable signal that UIKit discarded a cell, for example tying teardown to the cell's deallocation once the runtime's object lifetime model allows it.

When a cell leaves the table, ListView tears its item view down but kept
the cell's WeakRef to it and left the dead native root in the cell's
contentView. A later prepare of that cell re-attached the torn-down view,
whose graph was no longer rooted, so native parts of it (such as the
UIColor cached by a CSS Color) could already be released; the resulting
exception made the data source return a nil cell and UIKit abort. UIKit
also puts removed cells back into the table without asking for them
again, and those rows stayed blank. Both happen constantly while an
accessibility client (XCUITest, VoiceOver) disables cell reuse.

- detach the item's native root from the cell on teardown
- clear the cell's reference so the next prepare builds a new view
- prepare a cell that is back in the table without content, from
  didMoveToSuperview and from willDisplay; cells UIKit announces after
  taking them out of the table are left alone, since preparing them
  would keep them in the ListView's map for good
- the same teardown changes for sticky header views
@nx-cloud

nx-cloud Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 6400534

Command Status Duration Result
nx run-many --target=test --configuration=ci --... ✅ Succeeded <1s View ↗

💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-11-14 18:06:45 UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 14, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@nativescript/core@11642
npm i https://pkg.pr.new/@nativescript/vite@11642
npm i https://pkg.pr.new/@nativescript/webpack@11642

commit: 135d4ea

@edusperoni
edusperoni marked this pull request as ready for review November 14, 2026 15:33
Comment thread packages/core/ui/list-view/index.ios.ts Resolved
Comment thread packages/core/ui/list-view/index.ios.ts Resolved
Comment thread packages/core/ui/list-view/index.ios.ts Resolved
@NathanWalker
NathanWalker merged commit ce4102b into main Nov 14, 2026
8 of 9 checks passed
@NathanWalker
NathanWalker deleted the fix/ios-list-view-cell-teardown branch November 14, 2026 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants