Fix iOS pager crash: defer setViewControllers when called during active UIPageViewController transition.

This commit is contained in:
Brent Simmons committed 2026-05-18 16:41:21 -07:00
1 parent a8885d0444
commit b100374065
1 file changed
+24 -4
+24 -4
View File
@@ -33,6 +33,8 @@ final class ArticleViewController: UIViewController {
private var defaultControls: [UIBarButtonItem]?
private var pageViewController: UIPageViewController!
private var isPageTransitionInProgress = false
private var pendingSetViewController: WebViewController?
private var currentWebViewController: WebViewController? {
return pageViewController?.viewControllers?.first as? WebViewController
@@ -61,10 +63,17 @@ final class ArticleViewController: UIViewController {
if let controller = currentWebViewController, controller.article != article {
controller.setArticle(article)
DispatchQueue.main.async {
// You have to set the view controller to clear out the UIPageViewController child controller cache.
// You also have to do it in an async call or you will get a strange assertion error.
self.pageViewController.setViewControllers([controller], direction: .forward, animated: false, completion: nil)
if isPageTransitionInProgress {
// Calling setViewControllers during an active page transition trips a UIPageViewController
// internal assertion (NSInternalInconsistencyException) and crashes the app. Stash the
// controller and flush it from didFinishAnimating once the transition has ended.
pendingSetViewController = controller
} else {
DispatchQueue.main.async {
// You have to set the view controller to clear out the UIPageViewController child controller cache.
// You also have to do it in an async call or you will get a strange assertion error.
self.pageViewController.setViewControllers([controller], direction: .forward, animated: false, completion: nil)
}
}
}
updateUI()
@@ -495,7 +504,18 @@ extension ArticleViewController: UIPageViewControllerDataSource {
extension ArticleViewController: UIPageViewControllerDelegate {
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
isPageTransitionInProgress = true
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
isPageTransitionInProgress = false
if let pending = pendingSetViewController {
pendingSetViewController = nil
pageViewController.setViewControllers([pending], direction: .forward, animated: false, completion: nil)
}
guard finished, completed else { return }
guard let article = currentWebViewController?.article else { return }