QLPreviewPanel causing warnings with QLThumbnailImageCreate

I have an NSCollectionView, whose items contain an image which is created with QLThumbnailImageCreate.


Everything was working fine until I added in the ability to preview items using QLPreviewPanel.


As long as I don't invoke the QLPreviewPanel, I can reloadData() on the CollectionView without any problem but, as soon as I have invoked the QLPreviewPanel and hidden it again, calling reloadData() causes a slew of warnings to the effect of:


[QL] QLError(): Asking to compute a thumbnail in main thread is potentially blocking the user interface


… for every item in the collection view.


Here is the code I have added to the ViewController :


extension ViewController : QLPreviewPanelDataSource
{
  public override func acceptsPreviewPanelControl(_ panel: QLPreviewPanel!) -> Bool
  {
    return true
  }
  
  public override func beginPreviewPanelControl(_ panel: QLPreviewPanel!)
  {
    panel.dataSource = self
  }
  
  override func endPreviewPanelControl(_ panel: QLPreviewPanel!)
  {
    panel.dataSource = nil
  }

  func numberOfPreviewItems(in panel: QLPreviewPanel!) -> Int
  {
    return selectedItems.count
  }

  func previewPanel(_ panel: QLPreviewPanel!, previewItemAt index: Int) -> QLPreviewItem!
  {
    return selectedItems[index].url! as QLPreviewItem
  }
}


All in all, this API is very poorly documented. Anyone got any ideas?

Replies

Just reading the alert you get.


Did you try executing previewPanel(_ panel: QLPreviewPanel!, previewItemAt index: Int) in another thread ?

Nope. Just the code I cited for the panel and using QLThumbnailImageCreate for the images in the collection view items.


The problem is completely solved by setting the URL for the collection item from the collection view controller's data source, then calling QLThumbnailImageCreate on a background thread within the item, followed by updating the image view in the item on completion.


It's just weird that you can "abuse" QLThumbnailImageCreate in the main thread, as long as you never invoke a QLPreviewPanel 😕


Thanks for your input anyway. Let's hope this thread is useful to someone else.