如何从iOS 13中的NSFetchResultsController获取可区分的快照? [英] How to get a diffable snapshot from an NSFetchResultsController in iOS 13?

查看:75
本文介绍了如何从iOS 13中的NSFetchResultsController获取可区分的快照?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里是WWDC 2019视频230,大约从第14分钟开始,据称NSFetchedResultsController现在出售NSDiffableDataSourceSnapshot,因此我们可以将其直接应用于可扩散数据源(UITableViewDiffableDataSource)。

So here we are in WWDC 2019 video 230, and starting at about minute 14 it is claimed that NSFetchedResultsController now vends an NSDiffableDataSourceSnapshot, so we can just apply it directly to a diffable data source (UITableViewDiffableDataSource).

但这不是他们所说的,或者我们得到的。我们在委托方法 controller(_:didChangeContentWith:)中得到的是NSDiffableDataSourceReference。我们如何从中得到一个实际的快照,我的可扩散数据源通用类型应该是什么?

But this is not quite what they say, or what we get. What we get, in the delegate method controller(_:didChangeContentWith:), is an NSDiffableDataSourceReference. How do we get from this to an actual snapshot, and what should my diffable data source generic types be?

推荐答案

可扩散数据源应使用通用类型String和NSManagedObjectID声明。现在,您可以将引用转换为快照了:

The diffable data source should be declared with generic types String and NSManagedObjectID. Now you can cast the reference to a snapshot:

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference) {
    let snapshot = snapshot as NSDiffableDataSourceSnapshot<String,NSManagedObjectID>
    self.ds.apply(snapshot, animatingDifferences: false)
}

这就留下了如何填充单元格的问题。在可扩散数据源(在我的示例中为 self.ds )中,当您填充单元格时,返回获取的结果控制器并获取实际的数据对象。

This leaves open the question of how you're going to populate the cell. In the diffable data source (self.ds in my example), when you populate the cell, return to the fetched results controller and fetch the actual data object.

例如,在表格视图中,我在每个单元格中显示组的名称

For example, in my table view I am displaying the name of a Group in each cell:

lazy var ds : UITableViewDiffableDataSource<String,NSManagedObjectID> = {
    UITableViewDiffableDataSource(tableView: self.tableView) {
        tv,ip,id in
        let cell = tv.dequeueReusableCell(withIdentifier: self.cellID, for: ip)
        cell.accessoryType = .disclosureIndicator
        let group = self.frc.object(at: ip)
        cell.textLabel!.text = group.name
        return cell
    }
}()

这篇关于如何从iOS 13中的NSFetchResultsController获取可区分的快照?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆