Convert playlist metadata loader to use code blocks, which fixes references and stops crashes on adding new tracks.

CQTexperiment
Chris Moeller 2016-05-07 10:20:46 -07:00
parent f0bee5ea6c
commit ba9fe0534f
1 changed files with 17 additions and 53 deletions

View File

@ -457,19 +457,23 @@ NSMutableDictionary * dictionaryWithPropertiesOfObject(id obj, NSArray * filterL
{ {
if ([pe metadataLoaded]) continue; if ([pe metadataLoaded]) continue;
@autoreleasepool { __block PlaylistEntry *weakPe = pe;
NSInvocationOperation *readEntryInfoOperation; __block NSMutableDictionary *entryInfo = [NSMutableDictionary dictionaryWithCapacity:20];
readEntryInfoOperation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:@selector(readEntryInfo:)
object:pe];
[readEntryInfoOperation addObserver:self NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
forKeyPath:@"isFinished" NSDictionary *entryProperties = [AudioPropertiesReader propertiesForURL:weakPe.URL];
options:NSKeyValueObservingOptionNew if (entryProperties == nil)
context:NULL]; return;
[queue addOperation:readEntryInfoOperation];
} [entryInfo addEntriesFromDictionary:entryProperties];
[entryInfo addEntriesFromDictionary:[AudioMetadataReader metadataForURL:weakPe.URL]];
}];
[op setCompletionBlock:^{
[weakPe performSelectorOnMainThread:@selector(setMetadata:) withObject:entryInfo waitUntilDone:NO];
}];
[queue addOperation:op];
} }
[queue waitUntilAllOperationsAreFinished]; [queue waitUntilAllOperationsAreFinished];
@ -477,46 +481,6 @@ NSMutableDictionary * dictionaryWithPropertiesOfObject(id obj, NSArray * filterL
[playlistController performSelectorOnMainThread:@selector(updateTotalTime) withObject:nil waitUntilDone:NO]; [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 - (void)clear:(id)sender
{ {
[playlistController clear:sender]; [playlistController clear:sender];