UITableViewAlertForLayoutOutsideViewHierarchy error: Warning once only (iOS 13 GM) UITableViewAlertForLayoutOutsideViewHierarchy error: Warning once only (iOS 13 GM) ios ios

UITableViewAlertForLayoutOutsideViewHierarchy error: Warning once only (iOS 13 GM)


It happened to me because I registered the device for change orientation notification in the viewWillAppear(:) method.I moved the registration in the viewDidAppear(:) and Xcode it's not stopping at the breakpoint anymore.

What I can say is that layout changes might be run when the view is already visible...


Like @joe-h, I was getting this error and was also surprised as the unwind approach he shows is one used by lots of developers + is in some significant Apple iOS sample code.

The triggering line in my code (@joe-h, I'm guessing likely in yours, too) is a tableView.reloadRows at the selectedIndexPath (which is an unwrapped tableView.indexPathForSelectedRow):

tableView.reloadRows(at: [selectedIndexPath], with: .automatic)

Unfortunately commenting out the row isn't an option if you are unwinding after updating the value in an existing tableView row (which is an approach in the Apple FoodTracker tutorial mentioned above, as well as one used in Apple's Everyone Can Code series). If you don't reload the row(s) then your change won't show in the tableView. After commenting out the reload in the unwind, I added a viewDidAppear with the following code and this seems to fix things:

override func viewDidAppear(_ animated: Bool) {    super.viewDidAppear(animated)    if let selectedIndexPath = tableView.indexPathForSelectedRow {        tableView.reloadRows(at: [selectedIndexPath], with: .automatic)    }}

I'd welcome comments on whether this is a sound approach, but for now, this seems to be working.


I had the same error on my Project; A tableView with a diffable datasource. Been bugging on it for hours. Problem lies in updating the snapshot, more specifically on a background thread (default). Forcing the update of the datasource on the main thread got rid of the problem! Hope this helps someone out there!

    func updateData(on annotations: [Annotation]) {        var snapshot = NSDiffableDataSourceSnapshot<AnnotationType, Annotation>()        //Append available sections        AnnotationType.allCases.forEach { snapshot.appendSections([$0]) }        //Append annotations to their corresponding sections        annotations.forEach { (annotation) in            snapshot.appendItems([annotation], toSection: annotation.type as AnnotationType)        }        //Force the update on the main thread to silence a warning about tableview not being in the hierarchy!        DispatchQueue.main.async {            self.dataSource.apply(snapshot, animatingDifferences: true)        }    }