Thomas Tempelmann
Thomas Tempelmann

Reputation: 12125

Assertion error when using NSCollectionView with QLPreviewView

I am using an NSCollectionView where each NSCollectionViewItem uses a QLPreviewView to get a rendering of a file's content.

(This is an attempt at a file browser for images and other previewable files.)

Initially, this works fine.

However, once collection items are getting re-used, I get an assertion error (both in 10.13 and 10.14):

[QL] Assertion failure (unreachable code) - [… MyPreviewView activated … doc:[QLPreviewDocument …]] is already activated

Apparently, before I can re-use a NSCollectionViewItem, the previously used QLPreviewItem needs to be set to inactive state somehow. How do I do that?

I've tried to send the close message to the QLPreviewView instance but that doesn't make a difference.

I also do not get a dealloc call on my QLPreviewView subclass, which suggests that the object is still referenced by something else, possibly the QLPreviewDocument, which then gets confused about the change of state.

I have made a demo project available on github: https://github.com/tempelmann/NSCollectionViewWithQLPreview

To test: Run it, then scroll down. When reaching items 50 to 60, the assertion will be triggered.

Upvotes: 2

Views: 145

Answers (1)

Thomas Tempelmann
Thomas Tempelmann

Reputation: 12125

The fix is to set QLPrewiewView's shouldCloseWithWindow property to NO.

This, I suspect, tells the controller behind the scenes not to attach itself to higher level structures, i.e. tells it to remain self-sufficient.

So, adding this line to the code that sets up a new MyPrewiewView object in the sample code's ViewController.m file prevents the error:

qlView.shouldCloseWithWindow = NO;

Upvotes: 1

Related Questions