Reinhard Männer
Reinhard Männer

Reputation: 15217

UITableViewAlertForLayoutOutsideViewHierarchy error during batch update of UITableView with CATransaction

My app uses a tableView that is updated by batch updates.

The cells have an indicatorImage that depends on the section of the table. When a cell is moved between sections, this indicator changes, and I want to animate the change of the indicator image.
To do so, I use first tableView.performBatchUpdates to animate deletions, insertions, and movements of the cells.
Secondly, I embed the batch updates in a CATransaction block to animate the image change.

This works perfectly with one exception: If an alert should be shown during the table view update, my app stops at a UITableViewAlertForLayoutOutsideViewHierarchy breakpoint.

Here is the code:

CATransaction.begin()
tableView.performBatchUpdates({ 
    tableView.deleteRows(at: deletions, with: .automatic)
    tableView.insertRows(at: insertions, with: .automatic)
    for (from, to) in movements {
        tableView.moveRow(at: from, to: to)
        if from.section == kTableSectionBought {
            if let cell = tableView.cellForRow(at: from) {
                let indicatorImage = to.section < 2 ? self.progressImages!.first : self.progressImages!.last
                UIView.transition(with: cell.imageView!, // see <https://stackoverflow.com/a/40472115/1987726>
                                                    duration: 0.25,
                                                    options: .transitionCrossDissolve,
                                                    animations: { cell.imageView!.image = indicatorImage },
                                                    completion: nil)
            }
        }
    }
    animateCells(deletions: deletions, movements: movements)
}, completion: { (finished) in
    // some code
}  

My question is:

What could be the reason of the bug, and how can I avoid it?

PS: I have read this question, but it does not have an answer, and in contrast, my UIWindow is defined.

Upvotes: 1

Views: 147

Answers (0)

Related Questions