Created
December 4, 2019 14:57
-
-
Save michaelevensen/21211862f51566e352ff75544503a021 to your computer and use it in GitHub Desktop.
Dynamic updating of UITableView with FireStore changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func dataSourceForQuery(_ query: Query) -> CategorySessionsTableViewDataSource { | |
return CategorySessionsTableViewDataSource(query: query) { (changes, sessions) in | |
// Success: Perform batch remove / insert of cells | |
if #available(iOS 11.0, *) { | |
self.tableView.performBatchUpdates({ | |
self.performUpdates(for: changes) | |
}, completion: nil) | |
} | |
// Fallback on earlier versions | |
else { | |
self.tableView.beginUpdates() | |
self.performUpdates(for: changes) | |
self.tableView.endUpdates() | |
} | |
// Dismiss Activity Loader | |
self.dismissActivity() | |
} | |
} | |
/// Performs needed UI updates based on `DocumentChange`'s in TableView | |
fileprivate func performUpdates(for changes: [DocumentChange]) { | |
guard let totalRowCount = self.dataSource?.count else { | |
return | |
} | |
changes.forEach { (change) in | |
switch change.type { | |
case .added: | |
// Only insert if needed | |
if self.tableView.numberOfRows(inSection: 0) < totalRowCount { | |
self.tableView.insertRows(at: [IndexPath(row: Int(change.newIndex), section: 0)], with: .top) | |
} | |
case .removed: | |
self.tableView.deleteRows(at: [IndexPath(row: Int(change.oldIndex), section: 0)], with: .top) | |
case .modified: | |
self.tableView.reloadRows(at: [IndexPath(row: Int(change.newIndex), section: 0)], with: .automatic) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And here is
UICollectionView
: