Unexpected UI Freeze When Using NSCollectionView with Large Data Set on macOS 13.6
I recently switched to I'm a bit lost with I'm working with a important UI freeze when loading a large data set into an `NSCollectionView` on macOS 13.6. My collection view is supposed to display about 10,000 items, and while I have implemented pagination to load items in batches, the UI still becomes unresponsive when the view appears. I was expecting smoother scrolling and loading, especially since the data is loaded asynchronously. Here's a snippet of how Iām currently loading data: ```swift class MyCollectionViewController: NSViewController, NSCollectionViewDataSource { let itemsPerPage = 100 var currentPage = 0 var allItems: [MyItem] = [] // Assume this holds all items @IBOutlet weak var collectionView: NSCollectionView! override func viewDidLoad() { super.viewDidLoad() loadItems(for: currentPage) } func loadItems(for page: Int) { let startIndex = page * itemsPerPage let endIndex = min(startIndex + itemsPerPage, allItems.count) let itemsToDisplay = Array(allItems[startIndex..<endIndex]) collectionView.animator().reloadData() // This is where I think the scenario might be } func numberOfSections(in collectionView: NSCollectionView) -> Int { return 1 } func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int { return allItems.count } func collectionView(_ collectionView: NSCollectionView, itemForItemAt indexPath: IndexPath) -> NSCollectionViewItem { let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "ItemIdentifier"), for: indexPath) // Configure item here return item } } ``` I've tried optimizing my data source methods, and I even set `NSCollectionView` to use `NSCollectionViewFlowLayout` for better performance, but the question continues. The collection view freezes for a few seconds upon appearing, especially the first time it shows after loading new data. I get no behavior messages, and the CPU usage spikes during this freeze, which makes me think it's something related to how I'm updating the UI. Is there a more efficient way to manage a large data set in `NSCollectionView` that prevents these UI freezes? Any suggestions on improving the experience for the user would be appreciated. My development environment is Linux. Any ideas what could be causing this? Hoping someone can shed some light on this. This is my first time working with Swift 3.11. Am I missing something obvious? I'm working in a CentOS environment. What's the correct way to implement this? Any advice would be much appreciated.