Swiftpack.co - Package - michaelnisi/batchupdates

BatchUpdates

BatchUpdates makes updating UITableView and UICollectionView easier.

Resources

Example

Adopt the SectionedDataSource protocol with your UICollectionViewDataSource or UITableViewDataSource object.

import BatchUpdates

class SomeDataSource: SectionedDataSource {

  /// Enumerates item types provided by this data source, where `Product`
  /// would be some data type in your domain model.
  enum Item: Hashable {
    case product(Product)
    case message(String)
  }

  /// The changes block receives changes.
  var changesBlock: (([[Change<Item>]]) -> Void)?

}

A view controller would install the data source.

let dataSource = SomeDataSource()
collectionView.dataSource = dataSource

dataSource.changesBlock = { [weak self] changes in
  guard let cv = self?.collectionView else {
    return
  }

  self?.dataSource.commit(changes, performingWith: .collection(cv))
}

Your data source SomeDataSource would implement an API for communicating with your domain model and services, providing their data as sections of data source items enumerated by SomeDataSource.Item.

When the data source has received new data, it diffs old and new sections with its static func makeChanges(old: [Array<Item>], new: [Array<Item>]) -> [[Change<Item>]] function and submits changesBlock on the main queue, suggesting the changes without updating its internal state. The sections data structure remains untouched until the view controller commits the changes. This is when SectionedDataSource performs batch updates, updating the data model inside the updates block of performBatchUpdates(_:completion:) on UICollectionView or UITableView.

Documentation

Go take a look at this.

Install

📦 Add https://github.com/michaelnisi/batchupdates to your package dependencies.

License

MIT

Github

link
Stars: 1

Dependencies

Used By

Total: 0