36
votes

I am getting a strange error with iOS13 when performing a Segue and I can't figure out what it means, nor can I find any documentation for this error. The problem is that this seems to cause a lot of lag (a few seconds) until the segue is performed.

2019-09-11 22:45:38.861982+0100 Thrive[2324:414597] [TableView] Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes. Make a symbolic breakpoint at UITableViewAlertForLayoutOutsideViewHierarchy to catch this in the debugger and see what caused this to occur, so you can avoid this action altogether if possible, or defer it until the table view has been added to a window. Table view: ; layer = ; contentOffset: {0, 0}; contentSize: {315, 118}; adjustedContentInset: {0, 0, 0, 0}; dataSource: >

I am using Hero but I tried disabling it and using a regular Segue and this hasn't stopped the lag.

The code to initiate the segue is didSelectRowAt

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            selectedCell = realIndexFor(activeGoalAt: indexPath)
            performSegue(withIdentifier: "toGoalDetails", sender: nil)
        } else if indexPath.section == 1 {
            selectedCell = indexPath.row
            performSegue(withIdentifier: "toIdeaDetails", sender: nil)
        } else {
            selectedDecision = indexPath.row
            hero(destination: "DecisionDetails", type: .zoom)
        }
    }

And then none of the code in viewDidLoad or viewWillAppear from the destination VC affects this in any way (I tried commenting it all out with no difference.

Any idea what's causing this? I can share whatever other details are needed.

Thank you.

16
I see this too. It seems that you can ignore this.Paulw11
The problem is that I have a delay of several seconds between tapping on a cell and when it opens. Maybe it's not related.. Would you know where to look for what's causing the delay? I tried the Profiler but I couldn't find much.CristianMoisei
Normally delays are caused by updating UI elements when you are no running in the main queue. Do you perform any network operations or other asynchronous tasks? Make sure you dispatch back onto the main queue from any completion handlers.Paulw11
Thanks. I am calling a function that syncs with iCloud in the main thread but I tried moving it to global queue and it doesn't seem to make any difference. I've tried removing the call to the function and anything else to do with iCloud and just using local storage and it still makes no difference. What's also strange is that the sync only happens once on launch, but maybe I did something wrong. Can I ask what queue you think is best for syncing small iCloud files?CristianMoisei
What I noticed is that it registers my tap (prints a statement) but it doesn't start the animation for a few seconds.CristianMoisei

16 Answers

19
votes

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...

7
votes

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.

6
votes

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)
        }
    }
6
votes

This warning can happen du to updating table view or collection view while it is not visible, for example when it is on the parent view controller. To solve that, first, I created a property in the view controller, containing the table view to check if the view controller is visible or not, as bellow:

var isVisible: Bool = false

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.isVisible = true
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.isVisible = false
}

Then in the data source delegate, before reacting to changes, first check if the view controller is visible. If it was not, do not do any updates. For example

func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    guard isVisible else { return }
    tableView.beginUpdates()
}

You should check that visibility before doing any changes in the tableView. For example, in case of NSFetchedResultsController, it must be done in all delegate callbacks which we have implemented.

UPDATE

I recently found that if you update the table view with animation false, even when it is not visible, there won't be any warnings.

3
votes

In viewDidDisappear method I declare tableView.setContentOffset(CGPoint(x: 0, y: 0), animated: false) function. Some of you says it's not important but it affected tableView delegate methods. For example viewForHeader function is not called when I get this warning.

3
votes

For people using DiffableDataSource, set animatingDifferences to false and warning will be gone.

dataSource.apply(snapshot, animatingDifferences: false)

3
votes

I found the most robust and safe way is to wait for the didMoveToWindow of the table view / collection view

as even in viewWillAppear the view may NOT be attached to a window and puting your code in viewDidAppear may cause unwanted graphical glitches

class MyTableViewOrCollectionView: UITableView {

    var didMoveToWindowCallback: (()->())? = nil
    
    override func didMoveToWindow() {
        
        super.didMoveToWindow()
        
        didMoveToWindowCallback?()
        didMoveToWindowCallback = nil
        
    }

}

and than you can

override func viewDidLoad() {
    
    super.viewDidLoad()
    
    tableView.didMoveToWindowCallback = { [weak self] in
        self?.setupInitialContent()
    }
    
}
2
votes

I'm new to Xcode/Swift so this may or may not help anyone. I started getting this error after updating to iOS 13 and Xcode 11 within the app when going back to a list from a detail view.

I found that I was doing a tableView.reloadRows and tableView.insertRows in the unwind(as suggested by Apple in one of their tutorials)

@IBAction func unwindToMealList(sender: UIStoryboardSegue) {
    if let sourceViewController = sender.source as? MealViewController, let meal = sourceViewController.meal {

        if let selectedIndexPath = tableView.indexPathForSelectedRow {
            // Update an existing meal.
            meals[selectedIndexPath.row] = meal
            tableView.reloadRows(at: [selectedIndexPath], with: .none)
        }
        else {
            // Add a new meal.
            let newIndexPath = IndexPath(row: meals.count, section: 0)

            meals.append(meal)
            tableView.insertRows(at: [newIndexPath], with: .automatic)
        }
    }
}

)

I commented out that section of code and it went away.

Oddly enough leaving the sort and self.tableView.reloadData() didn't give me the error.

2
votes

iPadOS 13.2.3 swift 5.2 Xcode 11.2.1

Just ran into this issue only when starting the app while the device was landscape. I was calling the detail seque in the viewDidLoad func of the master controller to make sure the detail view was setup correctly.

     override func viewDidLoad() {
       super.viewDidLoad()
            ...
       self.performSegue(withIdentifier: "showDetail", sender: self)
     }

When I removed the performSeque the warning not longer appeared, however, the left bar buttons on the detail controller no longer worked properly, again only when starting the app while the device was landscape. The left most button would activate the next button to the right instead of what the first button was suppose to do.

The fix for the bar buttons was to add to the viewDidLoad

     override func viewDidLoad() {
       super.viewDidLoad()
            ...
       self.splitViewController?.preferredDisplayMode = UISplitViewController.DisplayMode.allVisible
     }

Then execute

     override func viewWillAppear(_ animated: Bool) {
       self.splitViewController?.preferredDisplayMode = UISplitViewController.DisplayMode.automatic
       super.viewWillAppear(animated)
     }

I have no explanation why this worked!

This app had worked flawlessly until iPados 13 was loaded.

2
votes

I am getting similar breakpoint with SwiftUI, without even dealing with viewDidLoad or viewDidappear

    //
//  ContentView.swift
//  DD
//
//  Created by Roman Emperor on 3/29/20.
//  Copyright © 2020 Emperors. All rights reserved.
//
import Combine
import SwiftUI

// Defining a class Booking of type Bindable Object [changed to ObservableObject]
class Booking: ObservableObject {
    var didChange = PassthroughSubject<Void, Never>()

    // Array of types to work with
    static let types = ["Consultation", "Tooth Pain", "Cleaning", "Brases", "Dental Implant" ]
    // Setting instance varibale type
    var type = 0 { didSet { update() } }

    func update () {
        didChange.send(())
    }
}


struct ContentView: View {
    @ObservedObject var booking = Booking() //bindableObject in old swift version

    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker(selection: $booking.type, label: Text("Select a Booking Type")) {
                        ForEach(0 ..< Booking.types.count){
                            Text(Booking.types[$0]).tag($0)
                        }
                    }
                }
            }
        .navigationBarTitle(Text("Darpan Dental Home"))
        }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

The Complete output Log is here:

*> 2020-03-29 09:22:09.626082+0545 DD[1840:76404] [TableView] Warning

once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes. Make a symbolic breakpoint at UITableViewAlertForLayoutOutsideViewHierarchy to catch this in the debugger and see what caused this to occur, so you can avoid this action altogether if possible, or defer it until the table view has been added to a window.*

**where is this UITableViewAlertForLayoutOutsideViewHierarchy in SwiftUI ? **

1
votes
extension UIView {

  func rootView() -> UIView {
     var view = self
     while view.superview.isNotNil {
         view = view.superview!
     }
     return view
  }

  var isOnWindow: Bool {
     return self.rootView() is UIWindow
    }
  }

then you just need to check if your tableView isOnWindow like...

if self.tableView.isOnWindow {
/// do stuff
}

Disclaimer: as the documentation explains, you may need to defer the call which means that there is no warranty your method will be called again so it's your responsibility to perform your update when isOnWindow is true.

1
votes

Had the same issue, removing tableView.reloadSections fixed it. This was the line of code causing the warning:

iOS 13:

tableView.reloadSections(IndexSet(integer: 0), with: .automatic)

in iOS 14, removing tableView.reloadSections did not fix the warning.

1
votes

Or maybe your code (like mine) has nothing wrong with it and this message just randomly starts popping up. In that case, do a clean on your project, restart Xcode and watch the message magically go away!

0
votes

Please check following function

override func viewWillLayoutSubviews()
0
votes

For anyone that has this issue with a UISplitViewController and a UITableView inside the detail view controller, you can try subclassing and override layoutSubviews like this (From this thread):

class CustomTableView: UITableView {
    override func layoutSubviews() {
        if (self.window == nil) {
            return
        }
        super.layoutSubviews()
    }
 }
0
votes

Instead of reloading the rows inside viewDidAppear, this is what it worked for me:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
    self.tableView.reloadRows(at: [indexPath], with: .none)
}

Also if you are using DiffableDataSource and you are selecting an indexPath manually for example, you need to do it on the completion block of the apply snapshot method:

dataSource.apply(snapshot, to: section, animatingDifferences: false, completion: {
    // select the indexPath programmatically or do UITableView UI stuff here.
})