Handle background track metadata loading in a saner manner.

CQTexperiment
Chris Moeller 2016-06-28 19:33:48 -07:00
parent ffa31e696d
commit 7876a4492f
1 changed files with 41 additions and 14 deletions

View File

@ -448,31 +448,58 @@ NSMutableDictionary * dictionaryWithPropertiesOfObject(id obj, NSArray * filterL
} }
//Clear the selection //Clear the selection
[playlistController setSelectionIndexes:nil]; [playlistController setSelectionIndexes:[NSIndexSet indexSet]];
[self performSelectorInBackground:@selector(loadInfoForEntries:) withObject:entries]; [self performSelectorInBackground:@selector(loadInfoForEntries:) withObject:entries];
return entries; return entries;
} }
- (void)loadInfoForEntries:(NSArray *)entries - (void)loadInfoForEntries:(NSArray *)entries
{ {
int processorCount = (int) [[NSProcessInfo processInfo] processorCount];
NSMutableArray *array = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < processorCount; ++i)
{
[array addObject:[[NSMutableArray alloc] init]];
}
i = 0;
for (PlaylistEntry *pe in entries) for (PlaylistEntry *pe in entries)
{ {
if ([pe metadataLoaded]) continue; if ([pe metadataLoaded]) continue;
int processor = i % processorCount;
++i;
[[array objectAtIndex:processor] addObject:pe];
}
if (!i) return;
for (NSArray *a in array)
{
if (![a count]) continue;
__block NSArray *weakA = a;
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
for (PlaylistEntry *pe in weakA)
{
__block PlaylistEntry *weakPe = pe; __block PlaylistEntry *weakPe = pe;
__block NSMutableDictionary *entryInfo = [NSMutableDictionary dictionaryWithCapacity:20]; __block NSMutableDictionary *entryInfo = [NSMutableDictionary dictionaryWithCapacity:20];
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{ NSDictionary *entryProperties = [AudioPropertiesReader propertiesForURL:pe.URL];
NSDictionary *entryProperties = [AudioPropertiesReader propertiesForURL:weakPe.URL];
if (entryProperties == nil) if (entryProperties == nil)
return; continue;
[entryInfo addEntriesFromDictionary:entryProperties]; [entryInfo addEntriesFromDictionary:entryProperties];
[entryInfo addEntriesFromDictionary:[AudioMetadataReader metadataForURL:weakPe.URL]]; [entryInfo addEntriesFromDictionary:[AudioMetadataReader metadataForURL:pe.URL]];
}];
[op setCompletionBlock:^{ dispatch_async(dispatch_get_main_queue(), ^{
[weakPe performSelectorOnMainThread:@selector(setMetadata:) withObject:entryInfo waitUntilDone:NO]; [weakPe setMetadata:entryInfo];
});
}
}]; }];
[queue addOperation:op]; [queue addOperation:op];