Search code examples
objective-cmacoscocoanscollectionviewnsprogressindicator

Is that possible to make a NSProgressIndicator spin while NSCollectionView is binding massive data?


I have a NSCollectionView and it need load about 1000 items (item only contains text, no image or other rich stuff).

I wanted to put a spinning NSProgressIndicator on the UI to let users know it is working, but found the indicator appear on UI and do not spin at all. In fact the whole UI is hung while the NSCollectionView is loading data, seems like NSCollectionView task occupies 100% of the main thread for that binding task.

Is that possible that I make a NSProgressIndicator spin while the binding happening?

EDIT:

I used a NSArrayController as the data source, and bind NSCollectionView's content to arrayController.arrangedObjects, when new items are added to arrayController, the collectionView is updated automatically.

If I put the binding task to a secondary thread, what I observe is each item appears on UI one by one and it takes about 0.5 second for each, since there are about 1000 items to be bound, the task takes way too long to complete.

What I am looking for is, when I want to bind new items to collectionView, the binding runs off the main thread with a progressIndicator spinning smoothly, and when the whole loading task is done, the collectionView is updated and progressIndicator is gone.


Solution

  • You need to move the NSCollectionView's item loading to a secondary thread so it's not blocking the main event loop (where the NSProgressIndicator timer is running). See the SourceView sample code project; specifically the gatherContents: method in the IconViewController.m source file.