TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Mar 2016 |
SPMSupports SPM | ✗ |
Maintained by Ivan Bruel.
DeltaCalculator is a Swift Library focused on replacing reloadData()
calls with animated insert, delete and move operations.
DeltaCalculator tries to optimize the number of iterations to calculate all the changes, making sure the UI thread doesn’t block.
This framework is based on BKDeltaCalculator Objective-C library.
DeltaCalculator is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'DeltaCalculator'
DeltaCalculator
compares two arrays and computes the delta between them, represented by a Delta
object. DeltaCalculator
is uses generics, therefor, you need to supply the class in the initializer.
In case your generic class is Equatable
you can initialize the calculator without an equalityTest
.
let deltaCalculator = DeltaCalculator<String>()
let delta = deltaCalculator.deltaFromOldArray(dataModel, toNewArray: newDataModel)
Otherwise you are required to supply an equalityTest
in the initializer.
let deltaCalculator = DeltaCalculator<NSDate>() { (lhs, rhs) -> Bool in
return lhs.compare(rhs) == .OrderedSame
}
let delta = deltaCalculator.deltaFromOldArray(dataModel, toNewArray: newDataModel)
To apply the Delta
to a UITableView
you need to use a batch update such as the following:
let delta = deltaCalculator.deltaFromOldArray(dataModel, toNewArray: newDataModel)
tableView.beginUpdates()
dataModel = newDataModel
delta.applyUpdatesToTableView(tableView, inSection: 0, withRowAnimation: UITableViewRowAnimation.Right)
tableView.endUpdates()
To apply the Delta
to a UICollectionView
you also need to perform it in a batch update:
let delta = deltaCalculator.deltaFromOldArray(dataModel, toNewArray: newDataModel)
collectionView.performBatchUpdates({
self.dataModel = newDataModel
delta.applyUpdatesToCollectionView(collectionView, inSection: 0)
}, completion: nil)
There is a sample project in the Example
directory.
Ivan Bruel, @ivanbruel
Delta is available under the MIT license. See the LICENSE file for more info.