DiffableDataSources

? A library for backporting UITableView/UICollectionViewDiffableDataSource.

Github星跟踪图


Introduction

Apple has announced a diffable data source at WWDC 2019.
It's a great API that easily updating our table view and collection view items using automatic diffing.
However, it's a little while before we can use it in a production service.
That because it requires the latest OS to use.
DiffableDataSources make it possible to introduce almost the same functionality from now on.

Uses a sophisticated open source DifferenceKit for the algorithm engine.
It's extremely fast and completely avoids synchronization bugs, exceptions, and crashes.


Difference from the Official

Spec

  • Supports iOS 9.0+ / macOS 10.11+ / tvOS 9.0+
  • Open sourced algorithm.
  • Duplicate sections or items are allowed.
  • Using performBatchUpdates for diffing updates.

Namings

DiffableDataSources have different class names to avoid conflicts with the official API.
Correspondence table is below., Official, Backported, :---------------------------------------------------------------------------, :------------------------------------, [NSDiffableDataSourceSnapshot][NSDiffableDataSourceSnapshot_doc], DiffableDataSourceSnapshot, [UITableViewDiffableDataSource][UITableViewDiffableDataSource_doc], TableViewDiffableDataSource, [UICollectionViewDiffableDataSource][UICollectionViewDiffableDataSource_doc], CollectionViewDiffableDataSource, [NSCollectionViewDiffableDataSource][NSCollectionViewDiffableDataSource_doc], CocoaCollectionViewDiffableDataSource, [NSDiffableDataSourceSnapshot_doc]: https://developer.apple.com/documentation/uikit/uitableviewdiffabledatasource
[UITableViewDiffableDataSource_doc]: https://developer.apple.com/documentation/uikit/uitableviewdiffabledatasource
[UICollectionViewDiffableDataSource_doc]: https://developer.apple.com/documentation/uikit/uicollectionviewdiffabledatasource
[NSCollectionViewDiffableDataSource_doc]: https://developer.apple.com/documentation/appkit/nscollectionviewdiffabledatasource


Getting Started

Build Project

$ git clone https://github.com/ra1028/DiffableDataSources.git
$ cd DiffableDataSources/
$ make setup
$ open DiffableDataSources.xcworkspace

Basic Usage

First, define the type representing section.
It should conforms to Hashable for identifies from the all sections.
Type of enum can used conveniently befause it conforms Hashable by default.

enum Section {
    case main
}

Then, define the item type conforms to Hashable.

struct User: Hashable {
    var name: String
}

Create a data source object, it will be set to table view automatically.
You should dequeue the non nil cells via closure.

final class UsersViewController: UIViewController {
    let tableView: UITableView = ...

    lazy var dataSource = TableViewDiffableDataSource<Section, User>(tableView: tableView) { tableView, indexPath, user in
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = user.name
        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    }
}

Manages and updates the data sources intuitively by intermediating DiffableDataSourceSnapshot.
The UI isn't updated until you apply the edited snapshot object.
Update the UI with diffing animation automatically calculated by applying an edited snapshot.

let users = [
    User(name: "Steve Jobs"),
    User(name: "Stephen Wozniak"),
    User(name: "Tim Cook"),
    User(name: "Jonathan Ive")
]

let snapshot = DiffableDataSourceSnapshot<Section, User>()
snapshot.appendSections([.main])
snapshot.appendItems(users)

dataSource.apply(snapshot) {
    // completion
}

Check the documentation for more detailed API.


Requirements

  • Swift 5.0+
  • iOS 9.0+
  • macOS 10.11+
  • tvOS 9.0+

Installation

CocoaPods

Add the following to your Podfile:

pod 'DiffableDataSources'

Carthage

Add the following to your Cartfile:

github "ra1028/DiffableDataSources"

Swift Package Manager

Add the following to the dependencies of your Package.swift:

.package(url: "https://github.com/ra1028/DiffableDataSources.git", from: "x.x.x")

Contributing

Pull requests, bug reports and feature requests are welcome ?
Please see the CONTRIBUTING file for learn how to contribute to DiffableDataSources.


Relations

DifferenceKit

A fast and flexible O(n) difference algorithm framework for Swift collection.

Carbon

A declarative library for building component-based user interfaces in UITableView and UICollectionView.


License

DiffableDataSources is released under the Apache 2.0 License.

主要指标

概览
名称与所有者ra1028/DiffableDataSources
主编程语言Swift
编程语言Swift (语言数: 3)
平台
许可证Apache License 2.0
所有者活动
创建于2019-06-14 22:19:57
推送于2023-04-11 23:18:42
最后一次提交2021-06-09 00:25:33
发布数5
最新版本名称0.5.0 (发布于 2021-06-09 00:26:00)
第一版名称0.1.0 (发布于 2019-06-24 12:03:58)
用户参与
星数854
关注者数12
派生数69
提交数48
已启用问题?
问题数21
打开的问题数13
拉请求数17
打开的拉请求数6
关闭的拉请求数1
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?