0

I am trying to save a playlist of songs created with the media picker. I tried to use the suggestion provided in Persist a MPMediaItemCollection Object Using NSUserDefaults. This solution uses NSKeyedArchiver as follows:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:mediaItemCollection];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:@"someKey"];
[defaults synchronize];

Then when the MPMediaItemCollection needs to be retrieved, the following is performed:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:@"someKey"];
MPMediaItemCollection *mediaItemCollection = [NSKeyedUnarchiver unarchiveObjectWithData:data];

While this seemed to work at first, I found that it doesn't work consistently over time. It seems that after my app is loaded after a new software release, some of the media items in the playlist are corrupted. For example, when I try to load the song title from the retrieved MPMediaItemCollection for display in a tableView as follows:

MPMediaItem *anItem = (MPMediaItem *)[mediaItemCollection.items objectAtIndex: row];

if (anItem) {
    cell.textLabel.text = [anItem valueForProperty:MPMediaItemPropertyTitle];

    if (cell.textLabel.text == nil) {
        NSString * persistentID = [anItem valueForProperty:MPMediaItemPropertyPersistentID];
        ...
}

the song title is nil for some of the entries. If I try to grab the persistentID for the song at this index in the queue, there is a persistentID, but it doesn't point to a valid song (so its probably just garbage).

SO THE QUESTION IS: Is it possible to save a MPMediaItemCollection that I can be sure is valid across all launches and software upgrades (both my upgrades and iOS upgrades). If so, how?

Community
  • 1
  • 1
JeffB6688
  • 3,782
  • 5
  • 38
  • 58

1 Answers1

0

The answer is not this way! the MPMediaItemCollection object cannot be converted to either NSArray, NSDictionary or any other object that we can archive in NSUserDefaults.

My suggestion is to enumerate through the collection, save an array of [anItem valueForProperty:MPMediaItemPropertyPersistentID] s. save it in the NSUserDefaults

then on the next run of the application, you read and enumerate through the NSArray and create the MPMediaItemCollection back with items with persistentIDs.

I hope it helps

Shahin
  • 865
  • 6
  • 21
  • ok thanks. I guess the link to http://stackoverflow.com/questions/5702707/play-ipod-playlist-retrieved-from-a-saved-persistentid-list provides the required implementations. The only thing that bothers me is a comment from Apple on persistentID. It says "The value is not guaranteed to persist across a sync/unsync/sync cycle." Do you have any idea what that means? – JeffB6688 Feb 11 '14 at 18:17
  • sure, this is true. it means if the user unsync the track from the device, and then resyncs the same track, the ID would change for the same music and this is ok. if you want to save a retainable array of musics during syncs, you may save an array of dictionary: @{"trackName" : @"", @"artistName": @""} instead of persistentID in userdefaults and query for the song eveytime you run the app. but I would not recommend this because it can be expensive when the songs count is high. – Shahin Feb 17 '14 at 09:46