views:

93

answers:

1

Hi.

I'm developing an iPhone application that uses the iPod library to play some songs. I load the songs with the code below. The problem is, when running this code right after the device has been synced with iTunes, there is a problem. Apparently the iPod Library needs to be updated, and it takes some time. If I go to the iPod Application right after a sync I seen a message saying "Updating Library..". If i call "[query items]" from my application while that is happening, I get an empty array indicating there is no songs in the library. Everything works perfect when the update is over. Is there any way to solve this problem? Maybe a way to detect when the update is over. I have tried to listen to alle NSNotifications, but none were called when the update finished.

    MPMediaQuery *query = [MPMediaQuery songsQuery];

 // convert all items to abstracted media item
 NSArray *items = [query items];

 NSMutableArray *convertedItems = [[NSMutableArray alloc] initWithCapacity:[items count]];
 for (MPMediaItem *item in items) {
  REMediaItem *mediaItem = [[REMediaItem alloc] initWithMediaItem:item];
  [convertedItems addObject:mediaItem];
  [mediaItem release];
 }

I hope someone can help.

Peter

+1  A: 

I discovered that there actually is a way to see when the update is complete. The device will post a notification when the update is over.

[[MPMediaLibrary defaultMediaLibrary] beginGeneratingLibraryChangeNotifications]
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self 
                       selector:@selector(iPodLibraryDidChange)
                           name: MPMediaLibraryDidChangeNotification 
                         object:nil];

The only problem is that I can't find a way to determinate if the device is updating the iPod Library and I should wait for it to finish or the device simply doesnt have any songs in the library. [query items] will return an empty array in both cases.

Peter