From b100374065d3d87aa7c8dae33a6e751bbe31eb4c Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 18 May 2026 16:41:21 -0700 Subject: [PATCH] Fix iOS pager crash: defer setViewControllers when called during active UIPageViewController transition. --- iOS/Article/ArticleViewController.swift | 28 +++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/iOS/Article/ArticleViewController.swift b/iOS/Article/ArticleViewController.swift index 5d64e0230..d5181ef7f 100644 --- a/iOS/Article/ArticleViewController.swift +++ b/iOS/Article/ArticleViewController.swift @@ -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 }