缓冲区 2.0

Buffer 2.0

测试已测试
语言语言 SwiftSwift
许可协议 MIT
发布时间最新发布2017年8月
SwiftSwift 版本3.1
SPM支持 SPM

Alex Usbergo 维护。




Buffer 2.0

Buffer

Swift μ-fame,用于高效数组差异、集合观察和数据源实现。

Swift 2.3 分支在这里

C++11 端口在这里

安装

如果您使用 CocoaPods

将以下内容添加到您的 Podfile

pod 'Buffer'

如果您使用 Carthage

要安装 Carthage,运行(使用 Homebrew)

$ brew update
$ brew install carthage

然后,将以下行添加到您的 Cartfile

github "alexdrone/Buffer" "master"    

入门

Buffer 被设计得非常具体,并且具有不同抽象程度的 API。

使用 Buffer 管理集合

您可以以下方式初始化和使用 Buffer

import Buffer

class MyClass: BufferDelegate {

  lazy var buffer: Buffer<Foo> = {
    // The `sort` and the `filter` closure are optional - they are a convenient way to map the src array.
    let buffer = Buffer(initialArray: self.elements, sort: { $0.bar > $1.bar }, filter: { $0.isBaz })
    buffer.delegate = self
  }()

  var elements: [Foo] = [Foo]() {
    didSet {
      // When the elements are changed the buffer object will compute the difference and trigger
      // the invocation of the delegate methods.
      // The `synchronous` and `completion` arguments are optional.
      self.buffer.update(with: newValues, synchronous: false, completion: nil)
    }
  }


  //These methods will be called when the buffer has changedd.

  public func buffer(willChangeContent buffer: BufferType) {
    //e.g. self.tableView?.beginUpdates()

  }

  public func buffer(didDeleteElementAtIndices buffer: BufferType, indices: [UInt]) {
    //e.g. Remove rows from a tableview
  }

  public func buffer(didInsertElementsAtIndices buffer: BufferType, indices: [UInt]) {
  }

  public func buffer(didChangeContent buffer: BufferType) {
  }

  public func buffer(didChangeElementAtIndex buffer: BufferType, index: UInt) {
  }
  
  public func buffer(didMoveElement buffer: BufferType, from: UInt, to: UInt) {
  }
  
  public func buffer(didChangeAllContent buffer: BufferType) {
  }
}

跟踪键路径

如果您的模型符合 KVO,您可以将一组键路径传递给您的缓冲区对象。当您的缓冲区对象管理的一个项目中的一个观察到的键路径发生变化时,将重新应用排序和过滤闭包(在后台线程上),计算差异并调用代理方法。

buffer.trackKeyPaths([\Foo.foo, \Bar.bar])

内置 UITableView 和 UICollectionView 适配器

Buffer 的主要用例之一可能是应用于 TableView 或 CollectionView。 Buffer 提供了 2 个适配器类,实现了 BufferDelegate 协议,并可以在需要时自动对目标 tableview/collectionview 进行所需的更改。

import Buffer

class MyClass: UITableViewController {

  lazy var buffer: Buffer<Foo> = {
    // The `sort` and the `filter` closure are optional - they are convenient way to map the src array.
    let buffer = Buffer(initialArray: self.elements, sort: { $0.bar > $1.bar }, filter: { $0.isBaz })
    buffer.delegate = self
  }()

  var elements: [Foo] = [Foo]() {
    didSet {
      // When the elements are changed the buffer object will compute the difference and trigger
      // the invocation of the delegate methods.
      // The `synchronous` and `completion` arguments are optional.
      self.buffer.update(with: newValues, synchronous: false, completion: nil)
    }
  }

  let adapter: TableViewDiffAdapter<Foo>!

  init() {
    super.init()
    self.adapter = TableViewDiffAdapter(buffer: self.buffer, view: self.tableView)

    // Additionaly you can let the adapter be the datasource for your table view by passing a cell
    // configuration closure to the adpater.
    adapter.useAsDataSource { (tableView, object, indexPath) -> UITableViewCell in
      let cell = tableView.dequeueReusableCellWithIdentifier("MyCell")
	  			cell?.textLabel?.text = object.foo
	  			return cell
    }
  }
  
}

组件化TableView

使用 Buffer 的另一种方便方法是通过 Buffer.TableView 类。这种抽象允许 tableView 在其状态(元素)变化时重新配置自己。

import Buffer

class ViewController: UIViewController {

  lazy var tableView: TableView<FooModel> = {
    let tableView = TableView<FooModel>()
    return tableView
  }()

  lazy var elements: [ListItem<FooModel>] = {
    var elements = [ListItem<FooModel>]()
    for _ in 0...100 {
      // AnyListItem wraps the data and the configuration for every row in the tableview.
      let item = ListItem(type: UITableViewCell.self,
                          container: self.tableView,
                          model: FooModel(text: "Foo"))) {
        cell, model in
        cell.textLabel?.text = model.text
      }
      elements.append(item)
    }
    return elements
  }()

  override func viewDidLayoutSubviews() {
    self.tableView.frame = self.view.bounds
  }

  override func viewDidLoad() {
    super.viewDidLoad()
    self.view.addSubview(self.tableView)
    self.tableView.elements = self.elements
  }
}

查看演示以了解更多关于 Buffer 的信息。

鸣谢