Alex Marchant
Alex Marchant

Reputation: 2510

NSCollectionView UI not updating when I using dispatch_async

So I have an NSCollectionView that has a list of songs. When I update that array that the NSCollectionView represents normally the UI reflects that change:

Picture: http://cl.ly/image/1D3A1o192k3W

Code:

[self setCurrentPlaylist:[HMRequest playlistPopular3Day]];

(Notes: _currentPlaylist is the array, [HMRequest playlistPopular3Day] returns an array of songs)

But when I try to use dispatch_async all of a sudden the UI goes nuts. The visible part of the NSCollectionView doesn't update, but when I scroll down, the previously hidden parts update correctly.

Picture (I've scrolled a bit, the blank white is what was previously visible, the songs are the part of the scroll view that was hidden): http://cl.ly/image/3y0f212Y3W3S

Code:

dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(aQueue, ^{        
    [self setCurrentPlaylist:[HMRequest playlistPopular3Day]];
});

Any help avoiding that blank UI section?

Upvotes: 0

Views: 493

Answers (1)

JustSid
JustSid

Reputation: 25318

Most parts of AppKit aren't threadsafe, so you should only make UI updates on the main thread and not on a background thread. Furthermore, the screenshot looks like you don't actually want to use a NSCollectionView but rather a NSTableView, which is much much faster in terms of performance.

Upvotes: 1

Related Questions