NSFetchedResultsControllerDelegate differable datasource code example

Example: UICollectionViewDiffableDataSource fetchedResultsController

let diffableDataSource = UICollectionViewDiffableDataSource(collectionView: collectionView) { (collectionView, indexPath, objectID) -> UICollectionViewCell? in
    guard let object = try? managedObjectContext.existingObject(with: objectID) else {
        fatalError("Managed object should be available")
    }

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell_identifier", for: indexPath)
    /// ... Setup cell with the managed object
    return cell
}

/// Store the data source in an instance property to make sure it's retained.
self.diffableDataSource = diffableDataSource

/// Assign the data source to your collection view.
collectionView.dataSource = diffableDataSource

Tags:

Misc Example