NSCollectionView Bug Under 10.12 Sierra?

I spent the last several hours trying to find out why a new desktop application that I'm developing crashes under macOS 10.12 Sierra right after it starts up. The application runs without a problem under 10.11. I've finally tracked the line where the application crashes under 10.12. This application uses NSCollectionView. And the application will crash inside NSCollectionView's itemForRepresentedObjectAt method. More precisely, it appears that the application tries to load the cell before applicationWillFinishLaunching. So if you create an array in applicationWillFinishLaunching and then use that array to prepare the collection view cell, the application will crash, I suppose. I have no doubts. Well, I write code in an old fashion - without using NSWindowController + NSViewController.


Does anybody have an application with NSCollectionView that crashes under macOS 10.12?

I'm pretty sure I noticed this when putting together a couple of simple sample projects with NSCollectionView (that is, that the datasource methods get fired prior to app did finish launching).


If your array is nil, when numberOfItemsInCollectionView: if your code was written in Objective-C, your app wouldn't crash because you can send messages to nil in Objective-C (you'd have to call reload data after you instantiated your array to get the collection view to populate). This seems to be a now overlooked advantage of Objective-C.


You can instantiate your array in init, instead of applicationDidFinishLaunching to ensure that your array gets created before the data source methods fire. Or...you could set the dataSource property in applicationDidFinishLaunching instead of wiring the outlet in IB too.

Thank you for your opinion.


I'm not looking for a workaround since I have one. I would like to know if that's a new norm starting with Sierra or actually a bug. If somebody confirms that it is indeed a bug, I might file a radar.

NSTableView documentation states this:


Important

It’s possible that your data source methods for populating the table view may be called before

awakeFromNib()
is called if the data source is specified in Interface Builder. You should defend against this by having the data source’s
numberOfRows(in:)
method return
0
for the number of rows when the data source has not yet been configured. In
awakeFromNib()
, when the data source is initialized you should always call
reloadData
on the table view.


So I guess NSCollectionView behaves the same way (though I don't believe it always did?)


I think it's a bit peculiar, especially if you are using Swift, that the data source methods would fire before the nib is loaded.

NSCollectionView Bug Under 10.12 Sierra?
 
 
Q