diff --git a/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift b/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift index 843d807e..ac862337 100644 --- a/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift +++ b/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift @@ -173,11 +173,6 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol cell.didDeselectCell(at: indexPath, delegateObject: delegateObject() as? MVMCoreUIDelegateObject, additionalData: nil) } - func performTableViewUpdates() { - tableView.beginUpdates() - tableView.endUpdates() - } - //-------------------------------------------------- // MARK: - MoleculeDelegateProtocol //-------------------------------------------------- @@ -187,7 +182,7 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol let point = molecule.convert(molecule.bounds.origin, to: tableView) if let indexPath = tableView.indexPathForRow(at: point), tableView.indexPathsForVisibleRows?.contains(indexPath) ?? false { - performTableViewUpdates() + refreshTable() } } @@ -305,25 +300,22 @@ extension MoleculeListTemplate: MoleculeListProtocol { } open func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], indexPath: IndexPath, animation: UITableView.RowAnimation?) { - // This dispatch is needed to fix a race condition that can occur if this function is called during the table setup. - DispatchQueue.main.async { - var indexPaths: [IndexPath] = [] + var indexPaths: [IndexPath] = [] - for molecule in molecules { - if let info = self.createMoleculeInfo(with: molecule) { - self.tableView?.register(info.class, forCellReuseIdentifier: info.identifier) - let index = indexPath.row + indexPaths.count - self.moleculesInfo?.insert(info, at: index) - indexPaths.append(IndexPath(row: index, section: 0)) - } + for molecule in molecules { + if let info = self.createMoleculeInfo(with: molecule) { + self.tableView?.register(info.class, forCellReuseIdentifier: info.identifier) + let index = indexPath.row + indexPaths.count + self.moleculesInfo?.insert(info, at: index) + indexPaths.append(IndexPath(row: index, section: 0)) } - - guard let animation = animation, - indexPaths.count > 0 else { return } - self.tableView?.insertRows(at: indexPaths, with: animation) - self.updateViewConstraints() - self.view.layoutIfNeeded() } + + guard let animation = animation, + indexPaths.count > 0 else { return } + self.tableView?.insertRows(at: indexPaths, with: animation) + self.updateViewConstraints() + self.view.layoutIfNeeded() } open func getIndexPath(for molecule: ListItemModelProtocol & MoleculeModelProtocol) -> IndexPath? { diff --git a/MVMCoreUI/BaseControllers/ProgrammaticTableViewController.swift b/MVMCoreUI/BaseControllers/ProgrammaticTableViewController.swift index 211426c3..d5a52d50 100644 --- a/MVMCoreUI/BaseControllers/ProgrammaticTableViewController.swift +++ b/MVMCoreUI/BaseControllers/ProgrammaticTableViewController.swift @@ -83,8 +83,11 @@ open class ProgrammaticTableViewController: ProgrammaticScrollViewController, UI } open func refreshTable() { - tableView.beginUpdates() - tableView.endUpdates() + // Perform asyncronously to ensure it can never run in the same process as table data updates. + DispatchQueue.main.async { [self] in + tableView.beginUpdates() + tableView.endUpdates() + } } /// For subclassing, returns the number of sections for table. This function calls numberOfSectionsForTableview aftre ensuring the table is setup properly. diff --git a/MVMCoreUI/BaseControllers/ThreeLayerTableViewController.swift b/MVMCoreUI/BaseControllers/ThreeLayerTableViewController.swift index 37de809f..369fb5ad 100644 --- a/MVMCoreUI/BaseControllers/ThreeLayerTableViewController.swift +++ b/MVMCoreUI/BaseControllers/ThreeLayerTableViewController.swift @@ -132,9 +132,7 @@ open class ThreeLayerTableViewController: ProgrammaticTableViewController { bottomViewTopConstraint?.constant = newSpace showFooter(width) } - DispatchQueue.main.async { - self.refreshTable() - } + refreshTable() } super.updateViewConstraints() } diff --git a/MVMCoreUI/Containers/Views/EntryFieldContainer.swift b/MVMCoreUI/Containers/Views/EntryFieldContainer.swift index 50236e45..9ccd1d25 100644 --- a/MVMCoreUI/Containers/Views/EntryFieldContainer.swift +++ b/MVMCoreUI/Containers/Views/EntryFieldContainer.swift @@ -270,9 +270,7 @@ import UIKit bottomBar?.frame = CGRect(x: 0, y: bounds.height - size, width: bounds.width, height: size) if updateMoleculeLayout || heightChanged { - DispatchQueue.main.async { [self] in - delegateObject?.moleculeDelegate?.moleculeLayoutUpdated(self) - } + delegateObject?.moleculeDelegate?.moleculeLayoutUpdated(self) } setNeedsDisplay()