首页 > 解决方案 > UICollectionViewDiffableDataSource cellProvider 比预期更频繁地调用

问题描述

我正在使用 UICollectionViewDiffableDataSource 来填充我的 UICollectionView。通过 REST API 接收到项目列表后,我创建一个新快照并像这样应用它:

DispatchQueue.main.async {
   var snapshot = NSDiffableDataSourceSnapshot<RegionSection, DiffableModel>()
   snapshot.appendSections(RegionSection.allCases)
   snapshot.appendItems(self.spotlights, toSection: .Spotlights)
   snapshot.appendItems(self.vendors, toSection: .Vendors)
   self.dataSource?.apply(snapshot, animatingDifferences: animated)
}

在 cellProvider 中设置单元格时,我从 URL 异步加载图像。我注意到,第一个单元格会疯狂地浏览所有加载的图像,并最终显示与预期不同的图像。(例如打算由最后一个单元格显示的图像)。

我决定调查并发现cellProvider 闭包的调用次数是预期的两倍。此外,collectionView.dequeueReusableCell 函数在调用的前半部分表现得很奇怪,因为它每次都返回相同的单元格,即使在 collectionView 中没有可以出队的单元格。

我的 cellProvider 关闭:

dataSource = UICollectionViewDiffableDataSource(collectionView: collectionView) { (collectionView, indexPath, entry) -> UICollectionViewCell? in
    if let spotlight = entry as? Spotlight{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "spotlightCell", for: indexPath) as! SpotlightCell
        
        cell.nameLabel.text = spotlight.title
        cell.subtitleLabel.text = spotlight.subtitle
        cell.categoryLabel.text = spotlight.type.getDescription().uppercased()
        cell.imageView.loadImage(fromUrl: spotlight.titlePictureUrl)
                    
        return cell
    }else if let vendor = entry as? Vendor{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "vendorCell", for: indexPath) as! VendorCell
        
        cell.nameLabel.text = vendor.title
        cell.assortmentLabel.text = vendor.assortmentDescription
        cell.imageView.loadImage(fromUrl: vendor.titlePictureUrl ?? vendor.pictureUrls?.first ?? "")
        
        if let distance = vendor.distance{
            cell.distanceLabel.text = (distance/1000) < 1 ? (distance.getReadableString(withDecimalSeparator: ",", andDecimalCount: 0) + "m entfernt") : ((distance/1000).getReadableString(withDecimalSeparator: ",", andDecimalCount: 0) + "km entfernt")
        }
                    
        return cell
    }
    return nil
}

这是一个例子:

  1. 我创建了一个包含 4 个供应商条目的快照(为简单起见,我没有在本示例的其他部分添加任何内容)
  2. cellProvider 被调用 4 次(对于每个 indexPath 和条目),每次出队的单元格都是同一个。
  3. cellProvider 又被调用了 4 次(同样,对于每个 indexPath 和条目),这一次单元格每次都不同。
  4. 每次调用 cellProvider 时,我都会调用 loadImage,它会尝试在我的图像缓存中为 URL 查找图像,如果找不到,则异步加载它。
  5. 由于所有调用几乎同时发生,每个图像都会加载两次,并且第一个单元格显示一个又一个图像,直到它启动的 4 个 URLSession 中的最后一个返回。

我无法想象 dataSource 经常将其称为 cellProvider 闭包的预期行为,我根本无法弄清楚为什么会发生这种情况或在文档中找到任何内容。

我希望有人可以向我解释为什么会发生这种情况,如果这是预期的行为,如何使用 DiffableDataSource 正确设置具有异步图像加载的单元格。


编辑:正如@Norb Braun 所建议的,对我有用的解决方案是对我的单元格使用绝对大小而不是估计大小!

标签: swiftuicollectionviewuikituicollectionviewcelluicollectionviewdiffabledatasource

解决方案


设置估计大小none为我解决了这个问题。当您需要使用自行调整大小的单元格时,此解决方案可能对您不起作用,但如果您的单元格保持相同大小而不管内容如何,​​您可以尝试一下。


推荐阅读