Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After further research and trial and error, I believe the fastest way is to query the media library using an <code>artistsQuery</code>, and instead of looping through each artist's collection, you keep track of the number of songs per artist using an NSMutableDictionary of NSNumbers.</p> <p>Using the code below, I'm seeing a speed improvement of 1.5x to 7x over my initial approach, depending on the device speed, number of artists, and number of songs per artist. (Biggest increase was an iPhone 3G which was taking 21.5 seconds for 945 songs initially, and now takes 2.7 seconds!) </p> <p>I will edit this answer if I discover any speed improvements. Please feel free to correct anything directly in my answer, as I am still new to Objective-C and the iOS APIs. (In particular, I might be missing a faster way to store integers in a hash table than what I've got below with NSNumbers in an NSMutableDictionary?)</p> <pre><code>NSMutableDictionary *artists = [[NSMutableDictionary alloc] init]; MPMediaQuery *query = [MPMediaQuery artistsQuery]; NSArray *items = [query items]; for (MPMediaItem *item in items) { NSString *artistName = [item valueForProperty:MPMediaItemPropertyArtist]; if (artistName != nil) { // retrieve current number of songs (could be nil) int numSongs = [(NSNumber*)[artists objectForKey:artistName] intValue]; // increment the counter (could be set to 1 if numSongs was nil) ++numSongs; // store the new count [artists setObject:[NSNumber numberWithInt:numSongs] forKey:artistName]; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload