Get SectionIdentifier from DiffableDataSource using IndexPath

When using either UICollectionViewDiffableDataSource or UITableViewDiffableDataSource, what is the "correct/safe" way to get the section identifier from an index path?


snapshot().sectionIdentifiers[indexPath.section]


or


guard let item = itemIdentifier(for: indexPath) else { return }
snapshot().sectionIdentifier(containingItem: item)

?

As far as I can tell, first is correct.


For the second, could it be that itemIdentifier(for: indexPath) be nil if section is empty ?

Not such a risk with the first


May look at this tutorial:

h ttps://medium.com/@jamesrochabrun/uicollectionviewdiffabledatasource-and-decodable-step-by-step-6b727dd2485

/* See LICENSE folder for this sample’s licensing information. Abstract: This sample's application delegate. */ import Cocoa @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { }

I'd love for this operation to be O(1) like itemIdentifier(for:):

This method is a constant time operation, O(1), which means you can look up an item identifier from its corresponding index path with no significant overhead.


I fear, perhaps unnecessarily, that snapshot() is an O(n) operation meaning retrieving the section identifier turns into an expensive operation.

Can someone from Apple chime in here?
Get SectionIdentifier from DiffableDataSource using IndexPath
 
 
Q