1

The following code, is used to partition and add sections to a list of songs.

    query = [MPMediaQuery songsQuery];
    [query addFilterPredicate: artistNamePredicate];            
    NSArray *itemsFromArtistQuery = [query items];  
    self.artist1 = [self partitionObjects:itemsFromArtistQuery collationStringSelector:@selector(title)];

Works perfectly. However when I try to do it with:

    query = [MPMediaQuery albumsQuery]; //same with playlistsQuery, artistsQuery, genresQuery
    [query addFilterPredicate: artistNamePredicate];            
    NSArray *itemsFromArtistQuery = [query collections];    
    self.artist1 = [self partitionObjects:itemsFromArtistQuery collationStringSelector:@selector(title)];

I get a SIGABRT error every single time. I've attributed it to the "collections" part of the code, as that's the only difference in the whole block. I've tried changing "title" to "name" "albumTitle", "playlist", "genre" and more, but I still end up with:

"-[MPConcreteMediaItemCollection title]: unrecognized selector sent to instance"

Can anybody help me here? I'm ready to rip my hair out!

THANK-YOU!

BenBen

topLayoutGuide
  • 1,407
  • 11
  • 22
  • Given the answer below, how are you dealing with the case where there are two songs with same name and all you have partitioned is an array of `NSStrings` (The only information you have in each row/section)? – sooper Mar 04 '12 at 01:06
  • This is rather old. The issue has been well and truly resolved, though I don't understand your question? Do you mean how am I managing selections with this partitioning method? – topLayoutGuide Mar 04 '12 at 05:01
  • Yes, for example if you were to list the album collections, how do you handle the case where there are two albums (different artists) with the same name, and all you have for reference is the album name stored in an array of `NSString`s? – sooper Mar 05 '12 at 13:52
  • It shouldn't make any difference. Doesn't impact my app at all. If you need more help, DM me on Twitter :) @cocotutch – topLayoutGuide Mar 18 '12 at 08:47
  • sooper Just predicate filter the album name. – topLayoutGuide May 31 '12 at 07:40

1 Answers1

2

You are right that the problem is with the collections part. The collationStringSelector: must be a method that returns a NSString for the objects your passing it, in this case MPMediaItemCollection's.

(It worked in the first case because you were passing MPMediaItem's which do respond to title).

Here we select each MPMediaItemCollection from the artistCollections array and then get a single MPMediaItem that represents the whole collection. We can then get the artists name and add it to an array.

query = [MPMediaQuery albumsQuery]; //same with playlistsQuery, artistsQuery, genresQuery
[query addFilterPredicate: artistNamePredicate];            
NSArray *artistCollections = [query collections];
NSMutableArray *artists = [NSMutableArray array];

for (MPMediaItemCollection *artist in artistCollections) {
    // get a single MPMediaItem that represents the collection
    MPMediaItem *representativeItem = [artist representativeItem];
    NSString *artistName = [representativeItem valueForProperty:MPMediaItemPropertyArtist];
    [artists addObject:artistName];
}

self.artist1 = [self partitionObjects:artists collationStringSelector:@selector(self)];

Now we are passing an array of NSString's so we set the collationStringSelector: to self which will return the artists name as a NSString.

steharro
  • 1,079
  • 8
  • 18
  • I did that, but it shows "Unknown Album" in every single cell? – topLayoutGuide Dec 30 '11 at 04:54
  • Just noticed it looks like you want to list all albums by a particular artist. In that case change `MPMediaItemPropertyArtist` to `MPMediaItemPropertyAlbumTitle`. Since the query is grouped in albums and limited to a particular artist by the predicate, the `representativeItem` for each group in the collection is an album not an artist. – steharro Dec 30 '11 at 12:44
  • I changed the code around to suit what I needed, thank-you. Still says "Unknown Album" in the cells though...I might have to check my CellForIndexRow method. – topLayoutGuide Dec 30 '11 at 14:31