3

My app uses the iPodMusicPlayer and, when suspended, the user might go out and make changes in Apple's Music App, for example creating or modifying a Playlist, then return to my App.

I receive the expected MPMediaLibraryDidChangeNotification, which is fine and I deal with it updating my references etc., but I receive a second MPMediaLibraryDidChangeNotification about 2 minutes later which I really don't need.

Any ideas on avoiding this second notification?

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification_iPodLibraryDidChange:) name: MPMediaLibraryDidChangeNotification object:nil];

[[MPMediaLibrary defaultMediaLibrary] beginGeneratingLibraryChangeNotifications];

5 Answers 5

3

The notification can be called multiple times depending on what's going on. For example, if you add an album to your phone with 12 songs in it, the notification gets called 12 times. Basically it gets called every time the library changes and not just when the sync has finished (at least on iOS 5.1, not sure about older iOS versions).

1

Where are you adding he observer? For example, if you add in the viewWillAppear and only remove observers in dealloc, you may have multiple observers which is causing a problem. At least, when I encountered a problem like this it was because I had inadvertently added a second observer without removing all the previous.

2 minutes seems like a long lag time (mine was a few seconds), but still may be worth checking out.

1
  • Thanks for the response Nathan. I'm adding it in viewDidLoad and removing it in viewDidUnload. The 2 minute time lag seems to correspond with the time taken to fire a notification after iOS is synced with iTunes on the Mac although in my case I am only making changes on the device.
    – amergin
    Jun 8, 2011 at 9:14
1

Probably the best way to avoid multiple launches of update procedures after multiple notifications is to set a timer and wait some seconds before performing the actual update.

0
if( !self.lastModifiedDate )        self.lastModifiedDate = [[NSDate alloc] init];
if( [self.lastModifiedDate compare:[[MPMediaLibrary defaultMediaLibrary] lastModifiedDate]] == NSOrderedSame )  return;
self.lastModifiedDate = [[MPMediaLibrary defaultMediaLibrary] lastModifiedDate];

The above lines in my notification handler method deal with the extra call. Still no idea why I'm getting it.

3
  • That will not fix it because each time the notification fires it compares the date to the second and thus, will always be different and never NSOrderedSame. I ended up maintaining a counter. If the @selector was called once, it's incremented. If the count is less than, it finishes the method. Else, it return;'s Pretty dirty but it works. Jan 9, 2014 at 10:42
  • The bug back in iOS 5 or whatever my answer was related to manifested in two notifications with identical lastModifiedDates. Didn't realise this was still out there as a bug.
    – amergin
    Jan 9, 2014 at 13:25
  • Funny. In iOS 7 it calls three times - once for Podcasts, once for Audiobooks and once for Music. All at different times. I just wanted it to call the once. Perchance you should update your answer? :P Jan 10, 2014 at 7:22
0

Remove the beginGeneratingLibraryChangeNotifications command, and it will fix it. :) You just get every notification for changed, one from the notification center, and one from the default library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.