RealmCollectionChange

@frozen
public enum RealmCollectionChange<CollectionType>

RealmCollectionChange值封装有关 Realm 通知报告的collection变更的信息。

更改信息有两种格式:集合中每种更改类型的行索引的简单数组,以及请求部分中适合直接传递给UITableView的批量更新方法的索引路径数组。

.update情况下的索引数组遵循UITableView的批处理约定,并且可以在转换为索引路径后按原样传递给表视图的批量更新函数。 例如,对于简单的单节表视图,您可以执行以下操作:

self.notificationToken = results.observe { changes in
    switch changes {
    case .initial:
        // Results are now populated and can be accessed without blocking the UI
        self.tableView.reloadData()
        break
    case .update(_, let deletions, let insertions, let modifications):
        // Query results have changed, so apply them to the TableView
        self.tableView.beginUpdates()
        self.tableView.insertRows(at: insertions.map { IndexPath(row: $0, section: 0) },
           with: .automatic)
        self.tableView.deleteRows(at: deletions.map { IndexPath(row: $0, section: 0) },
           with: .automatic)
        self.tableView.reloadRows(at: modifications.map { IndexPath(row: $0, section: 0) },
           with: .automatic)
        self.tableView.endUpdates()
        break
    case .error(let err):
        // An error occurred while opening the Realm file on the background worker thread
        fatalError("\(err)")
        break
    }
}
  • .initial 表示query的初始运行已完成(如果适用),现在可以使用collection,而无需执行任何阻塞工作。

    声明

    迅速

    case initial(CollectionType)
  • .update 表示已提交写事务(write transaction),该事务更改了collection中的对象,和/或修改了collection中的一个或多个对象。

    所有三个变更数组始终按升序排序。

    声明

    迅速

    case update(CollectionType, deletions: [Int], insertions: [Int], modifications: [Int])

    参数

    deletions

    在此版本中删除的collection上一版本中的索引。

    insertions

    此版本中添加的新collection中的索引。

    modifications

    在此版本中修改的新集合中对象的索引。

  • 如果发生错误,则会调用一次通知块,并返回.error结果和包含错误详细信息的NSError 。目前只有在后台线程上打开 Realm 以计算变更集失败时才会发生这种情况。使用 .error 值调用回调后,将永远不会再次调用该回调。

    声明

    迅速

    case error(Error)