From c1e7f11a1a23e00ab683ae33fc7d406ccc75aeef Mon Sep 17 00:00:00 2001 From: Morten Heiberg Date: Tue, 11 Dec 2018 10:26:03 +0100 Subject: [PATCH] Optimization. Ignore empty delta updates. (#5) --- .../CollectionViewDataSource.swift | 2 +- Sources/UITableView/TableViewDataSource.swift | 2 +- Sources/Updates.swift | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Sources/UICollectionView/CollectionViewDataSource.swift b/Sources/UICollectionView/CollectionViewDataSource.swift index a446575..be15b58 100644 --- a/Sources/UICollectionView/CollectionViewDataSource.swift +++ b/Sources/UICollectionView/CollectionViewDataSource.swift @@ -21,7 +21,7 @@ open class CollectionViewDataSource: NSObject, UICollectionViewDataSourc super.init() self.subscription = dataSource.updateHandler.subscribe { [weak self] update in - guard let `self` = self, !self.ignoreDataSourceUpdates else { return } + guard let `self` = self, !update.isEmpty, !self.ignoreDataSourceUpdates else { return } viewUpdate(update) } } diff --git a/Sources/UITableView/TableViewDataSource.swift b/Sources/UITableView/TableViewDataSource.swift index aa50f1b..41f6896 100644 --- a/Sources/UITableView/TableViewDataSource.swift +++ b/Sources/UITableView/TableViewDataSource.swift @@ -21,7 +21,7 @@ open class TableViewDataSource: NSObject, UITableViewDataSource where DS super.init() self.subscription = dataSource.updateHandler.subscribe { [weak self] update in - guard let `self` = self, !self.ignoreDataSourceUpdates else { return } + guard let `self` = self, !update.isEmpty, !self.ignoreDataSourceUpdates else { return } viewUpdate(update) } } diff --git a/Sources/Updates.swift b/Sources/Updates.swift index 19ce845..378af7b 100644 --- a/Sources/Updates.swift +++ b/Sources/Updates.swift @@ -10,6 +10,24 @@ public enum IndexedUpdate { deletedRows: [IndexPath] ) case full + + /// Whether the update is considered empty. + /// + /// - `true` for delta updates with no content + /// - `false` in all other cases + public var isEmpty: Bool { + switch self { + case let .delta(insertedSections, updatedSections, deletedSections, insertedRows, updatedRows, deletedRows): + return insertedSections.isEmpty && + updatedSections.isEmpty && + deletedSections.isEmpty && + insertedRows.isEmpty && + updatedRows.isEmpty && + deletedRows.isEmpty + case .full: + return false + } + } } public class IndexedUpdateHandler {