If you have ever wanted to repeatedly search an iOS device’s media items you might have found that the more items in the collection the slower your search loop will be. What I have found is that generally speaking instantiating the query itself is nearly instant.
However, any time you perform a valueForProperty on an MPMediaItem – you’re adding to the slowdown. It seems that this is quick operation on it’s own (quick enough) can really add up when run in a loop. The larger the collection, the longer it will take.
I’ve tested on a collection of about 1,800 items and it could potentially take 1-2 minutes to loop through everything in a search, depending on the specifics. That’s not good. Tried CoreData and it didn’t seem to be a ton better either.
My Initial Solution.
Create a few NSMutableDictionaries or perhaps a single über one & loop through collecting all the information you’ll ultimately need to search through. Do this up front.
In my experience with the 1,800 items, it took about 5 seconds to create these dictionaries & then it was done. Searching for artist, album, and genre now takes under one second. Searching for a song takes around six seconds. That’s much improved over what I was experiencing before.
It’s a little more upfront work but it pays for itself easily. You’ll balloon your memory footprint doing this, but it shouldn’t be too terribly bad.