Question

I have a NSCollectionView where one of the subviews is an NSImageView. The actual image requires loading from a webserver after going through an API call (so actually two network requests). Obviously I need to load the image asynchronously. I can bind the NSImageView's value to an NSValueTransformer that turns the model id into NSImage, but how do I implement the NSValueTransformer? It expects to return immediately from its transformedValue: method, whereas I can only fire off the network request there. I don't want to block the thread using synchronous networking.

Was it helpful?

Solution

Rather than using a NSValueTransformer which will actually return immediately, subclass the collection view item. For each item fire the transaction in this subclasses asynchronusly. Keep a IBOutlet for the NSImage in your subclass so that when the response is received from the server jus set the Image using this IBOulet. With this approach when the item is created, the corresponding view controller comes into picture. Subclassing it gives you control over the view and how to load it.

I hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top