Repository navigation
Conversation
UIKit can be enumerating the parent view controller's children when the provider's view callbacks run, e.g. while it propagates safe area insets after a status bar change and a synchronous React mount adds a pager. Calling addChild/removeFromParent there throws "Collection was mutated while being enumerated". Attach, move and detach on the next main queue turn instead.
There was a problem hiding this comment.
🟡 Changes recommended
A child-count change can rehost a retained inner pager before its controller containment is updated, potentially triggering a UIViewControllerHierarchyInconsistency crash.
1 open issue
What changed in this PR
This PR addresses #1267 by moving iOS pager controller attachment and removal outside view callbacks, avoiding mutations while UIKit traverses the controller hierarchy during safe-area updates.
Changes:
- Defers and coalesces containment updates on the main queue, resolving the target parent when they run.
- Keeps hosting-view insertion immediate, including in controller-free window overlays.
- Retains the hosting controller until deferred cleanup completes, even if the provider is deallocated.
| File | Description |
|---|---|
ios/PagerViewProvider.swift |
Replaces synchronous containment updates with deferred parent synchronization and cleanup. |
Review effort: Balanced
| super.willMove(toWindow: newWindow) | ||
| if newWindow != nil { | ||
| syncParentViewController(to: reactViewController()) | ||
| scheduleParentViewControllerSync() |
There was a problem hiding this comment.
Good catch, confirmed. With the first version, the nested-pager screen I added to the repro (outer pager whose page count changes, inner pagers in each page) crashed in _associatedViewControllerForwardsAppearanceCallbacks:performHierarchyCheck:isRoot: from _UIHostingView.swiftui_insertManagedSubview, which is #1098.
Fixed in 89afa05. Moving the hosting controller from one parent to another is synchronous again, the same as before this PR. Only attaching it to its first parent and detaching it are deferred, which are the calls that hit the enumeration crash. A hosting controller without a parent passes UIKit's hierarchy check, so deferring those is safe.
Re-ran the repro on an iPad simulator (iOS 27.2):
| nested pagers (add/remove pages) | status bar + remount | |
|---|---|---|
| 10.0.3 | ✅ | ❌ crash |
| first version | ❌ crash (#1098) | ✅ |
| 89afa05 | ✅ | ✅ |
_printHierarchy also shows every pager hosting controller appeared, with the right parent, after push/pop, remounts and background/foreground.
UIKit checks the hosting controller's parent as soon as its view moves to a window, so moving it into a page controller that SwiftUI rebuilt around a nested pager can't wait (callstack#1098). Only attaching to the first controller and detaching are deferred.
|
@apex-sentinel-staging review |
Apex Sentinel · ✅ Approve · review of
|
| Finding | Dismissed by | |
|---|---|---|
| 🟡 | Coalescing flag discards updateParentViewController requests that carry a specific parent | @troZee |
To raise one again, reply @apex-sentinel-staging note ... in its thread. It stays dismissed only if Sentinel's re-check withdraws it.
2 findings below minor hidden (user_settings.min_severity).
Reviewed with this repository's own Sentinel configuration (3a44ca5c9446).
💬 Talking to Sentinel
In a finding's thread, reply with:
| Command | What it does |
|---|---|
@apex-sentinel-staging note <context> |
Adds context; Sentinel re-checks the cited code |
@apex-sentinel-staging pivot <why> |
Says the finding is wrong; Sentinel re-checks the cited code |
@apex-sentinel-staging like |
Marks the finding as useful |
@apex-sentinel-staging dislike <why> |
Marks it as not useful, so it is not raised again |
On this conversation, comment @apex-sentinel-staging review to run a full review once again.
There was a problem hiding this comment.
Apex Sentinel: 1 new finding on 89afa05. The verdict and the full list are in the summary comment.

Summary
Fixes #1267.
PagerViewProvideradds itsUIHostingControlleras a child of the nearest view controller, and moves or removes it, from inside view callbacks:didMoveToWindow,willMove(toWindow:),willMove(toSuperview:),didMoveToSuperviewandlayoutSubviews. Those callbacks can run while UIKit is enumerating that view controller'schildren. For example:_recursiveUpdateContentOverlayInsetsFromParentIfNecessary).SafeAreaViewsends anunstable_Immediatestate update, which commits and mounts the React tree synchronously.addChildruns mid-enumeration, and UIKit throwsCollection <__NSArrayM> was mutated while being enumerated.The issue has the full lldb stack and a repro.
This PR keeps adding the hosting view to the provider immediately, so nothing renders late. Every containment change now goes through
updateParentViewController(to:):layoutSubviewscosts nothing.The behavior from #1204 stays the same: off-window the controller is detached, and inside window-level containers such as
FullWindowOverlay, where there's no view controller in the responder chain, it isn't attached to anything.Test Plan
Repro: https://github.com/mozzius/pager-view-statusbar-crash-repro (Expo SDK 58, RN 0.88.1, react-native-screens 4.30.1, iPad (A16) simulator, iOS 27.2). Each press of Crash shows or hides the status bar and remounts a
PagerViewthat sits outside the navigator._printHierarchyshows every pager hosting controllerappeared, with the right parent, after cold launch, push/pop, remounts and background/foreground. That's the same as 10.0.3, and old controllers don't pile up.What's required for testing (prerequisites)?
An iPad or iPhone SE simulator. On Face ID iPhones, hiding the status bar doesn't change the top safe area, so the walk doesn't happen.
What are the steps to reproduce (after prerequisites)?
Run the repro and press Crash a few times.
Compatibility
Checklist
README.md(not needed)