From ba9fe0534f7e1413c15c6723341b82e859ff49d0 Mon Sep 17 00:00:00 2001 From: Chris Moeller Date: Sat, 7 May 2016 10:20:46 -0700 Subject: [PATCH] Convert playlist metadata loader to use code blocks, which fixes references and stops crashes on adding new tracks. --- Playlist/PlaylistLoader.m | 70 ++++++++++----------------------------- 1 file changed, 17 insertions(+), 53 deletions(-) diff --git a/Playlist/PlaylistLoader.m b/Playlist/PlaylistLoader.m index 6c2778f7e..37f710c6f 100755 --- a/Playlist/PlaylistLoader.m +++ b/Playlist/PlaylistLoader.m @@ -456,20 +456,24 @@ NSMutableDictionary * dictionaryWithPropertiesOfObject(id obj, NSArray * filterL for (PlaylistEntry *pe in entries) { if ([pe metadataLoaded]) continue; + + __block PlaylistEntry *weakPe = pe; + __block NSMutableDictionary *entryInfo = [NSMutableDictionary dictionaryWithCapacity:20]; - @autoreleasepool { - NSInvocationOperation *readEntryInfoOperation; - readEntryInfoOperation = [[NSInvocationOperation alloc] - initWithTarget:self - selector:@selector(readEntryInfo:) - object:pe]; - - [readEntryInfoOperation addObserver:self - forKeyPath:@"isFinished" - options:NSKeyValueObservingOptionNew - context:NULL]; - [queue addOperation:readEntryInfoOperation]; - } + NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{ + NSDictionary *entryProperties = [AudioPropertiesReader propertiesForURL:weakPe.URL]; + if (entryProperties == nil) + return; + + [entryInfo addEntriesFromDictionary:entryProperties]; + [entryInfo addEntriesFromDictionary:[AudioMetadataReader metadataForURL:weakPe.URL]]; + }]; + + [op setCompletionBlock:^{ + [weakPe performSelectorOnMainThread:@selector(setMetadata:) withObject:entryInfo waitUntilDone:NO]; + }]; + + [queue addOperation:op]; } [queue waitUntilAllOperationsAreFinished]; @@ -477,46 +481,6 @@ NSMutableDictionary * dictionaryWithPropertiesOfObject(id obj, NSArray * filterL [playlistController performSelectorOnMainThread:@selector(updateTotalTime) withObject:nil waitUntilDone:NO]; } -- (NSDictionary *)readEntryInfo:(PlaylistEntry *)pe -{ - // Just setting this to 20 for now... - NSMutableDictionary *entryInfo = [NSMutableDictionary dictionaryWithCapacity:20]; - NSDictionary *entryProperties; - entryProperties = [AudioPropertiesReader propertiesForURL:pe.URL]; - if (entryProperties == nil) - return nil; - - [entryInfo addEntriesFromDictionary:entryProperties]; - [entryInfo addEntriesFromDictionary:[AudioMetadataReader metadataForURL:pe.URL]]; - return entryInfo; -} - -- (void)observeValueForKeyPath:(NSString *)keyPath - ofObject:(id)object - change:(NSDictionary *)change - context:(void *)context -{ - // We finished reading the info for a playlist entry - if ([keyPath isEqualToString:@"isFinished"] && [object isFinished]) - { - // stop observing - [object removeObserver:self forKeyPath:keyPath]; - - // get the playlist entry that the operation read for - PlaylistEntry *pe = nil; - [[object invocation] getArgument:&pe atIndex:2]; - [pe performSelectorOnMainThread:@selector(setMetadata:) withObject:[object result] waitUntilDone:NO]; - - } - else - { - [super observeValueForKeyPath:keyPath - ofObject:object - change:change - context:context]; - } -} - - (void)clear:(id)sender { [playlistController clear:sender];