title: What's the fastest way in iOS to retrieve the number of songs for a specific artist?
body: <p>I am trying to retrieve all the artists on the local iOS device, and for each artist, the number of songs available for that artist.</p>
  
  <p>I currently am doing this in a straight-forward fashion, by querying for all artists, and for each artist, counting the number of items (songs) in its collection:</p>
  
  <pre><code>MPMediaQuery *query = [[MPMediaQuery alloc] init];
  [query setGroupingType:MPMediaGroupingArtist];
  NSArray *collections = [query collections];
  for (MPMediaItemCollection *collection in collections)
  {
      MPMediaItem *representativeItem = [collection representativeItem];
      int songs = [[collection items] count];
      // do stuff here with the number of songs for this artist
  }
  </code></pre>
  
  <p>However, this doesn't seem very efficient, or at least, it's slower than I had anticipated.</p>
  
  <p>On a demo iPhone 4 device with several hundred artists, the above code took about 7 seconds to run. When I commented out the line that obtains the count of "collection items," the time is reduced to 1 second. </p>
  
  <p>So I am wondering if there is a faster way to retrieve the song counts for artists than what I am doing above?</p>
  
  <hr>
  
  <p><strong>Update 09/27/2011.</strong> I see that I can simplify the song count retrieval for an artist using this:</p>
  
  <pre><code>int songs = [collection count];
  </code></pre>
  
  <p>Rather than what I was doing:</p>
  
  <pre><code>int songs = [[collection items] count];
  </code></pre>
  
  <p>However, in reality this has had little effect on performance.</p>
  
  <p>I borrowed an iPhone 3G to try the performance of this issue on a slower device. </p>
  
  <p>My code takes 17.5 seconds to run on this 3G with only 637 songs spread across 308 artists.</p>
  
  <p>If I comment out the line that retrieves the number of songs, the same device finishes in only 0.7 seconds...</p>
  
  <p>There must be a faster way to retrieve number of songs per artist on an iOS device.</p>