The perils of digging into an MPMediaQuery (speed matters)

Hands dirty

Getting your hands dirty is better when it happens quicker.

The Problem. 

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.

Related Posts Plugin for WordPress, Blogger...

One thought on “The perils of digging into an MPMediaQuery (speed matters)

  1. tim

    i found this post and am wondering if you could give more information on this ?

    I’m running into the same speed/performance issues.

    Could you explain your NSMutableDictionaries solution ?




Leave a Reply

Your email address will not be published. Required fields are marked *

× 8 = fifty six

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>