I am trying to retrieve the user's playlists. I referred to the sample Guess The Intro from cocoalibspotify, however I am only getting 2 tracks of which are from the starred tracks. Is there anything that I missed? Also, can api/toplists be an alternative for this? I am more used to consuming json data so if there is an alternative to cocoalibspotify for retrieving the user's playlists, I would like to give it a try.

-(NSArray *)tracksFromPlaylistItems:(NSArray *)items {

NSMutableArray *tracks = [NSMutableArray arrayWithCapacity:items.count];

for (SPPlaylistItem *anItem in items) {
    if (anItem.itemClass == [SPTrack class]) {
        [tracks addObject:anItem.item];
    }
}

return [NSArray arrayWithArray:tracks];
}
-(void)getMyTracks {
    [SPAsyncLoading waitUntilLoaded:[SPSession sharedSession] timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *loadedSession, NSArray *notLoadedSession) {
        NSLog(@"SPOTIFY [%@ %@]: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), @"Session loaded.");

        [SPAsyncLoading waitUntilLoaded:[SPSession sharedSession] timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *loadedContainers, NSArray *notLoadedContainers) {
            NSLog(@"[%@ %@]: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), @"Container loaded.");

            NSMutableArray *playlists = [NSMutableArray array];
            [playlists addObjectsFromArray:[SPSession sharedSession].userPlaylists.playlists];
            [playlists addObject:[SPSession sharedSession].starredPlaylist];
            [playlists addObject:[SPSession sharedSession].inboxPlaylist];
            //[playlists addObjectsFromArray:[SPSession sharedSession].userPlaylists.flattenedPlaylists];

            [SPAsyncLoading waitUntilLoaded:playlists timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *loadedPlaylists, NSArray *notLoadedPlaylists) {
                // All of our playlists have loaded their metadata — wait for all tracks to load their metadata.
                NSLog(@"[%@ %@]: %@ of %@ playlists loaded.", NSStringFromClass([self class]), NSStringFromSelector(_cmd),
                      [NSNumber numberWithInteger:loadedPlaylists.count], [NSNumber numberWithInteger:loadedPlaylists.count + notLoadedPlaylists.count]);
                NSArray *playlistItems = [loadedPlaylists valueForKeyPath:@"@unionOfArrays.items"];
                NSArray *tracks = [self tracksFromPlaylistItems:playlistItems];

                [SPAsyncLoading waitUntilLoaded:tracks timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *loadedTracks, NSArray *notLoadedTracks) {
                    // All of our tracks have loaded their metadata. Hooray!
                    NSLog(@"[%@ %@]: %@ of %@ tracks loaded.", NSStringFromClass([self class]), NSStringFromSelector(_cmd),
                          [NSNumber numberWithInteger:loadedTracks.count], [NSNumber numberWithInteger:loadedTracks.count + notLoadedTracks.count]);
                    NSMutableArray *theTrackPool = [NSMutableArray arrayWithCapacity:loadedTracks.count];
                    for (SPTrack *track in loadedTracks) {
                        if (track.availability == SP_TRACK_AVAILABILITY_AVAILABLE && [track.name length] > 0) {

                            NSLog(@"TRACK %@ %@ %@",track.name,track.artists,track.album.name);
                        }
                    }
                }];
            }];
        }];
    }];
}
有帮助吗?

解决方案

Guess the Intro doesn't try to load all playlists - it just takes what it can in a few seconds and goes with it.

Loading the entire playlist tree (especially if you're also trying to load all the tracks) can take quite a while in CocoaLibSpotify, especially on first run since nothing is cached.

Typically, for UI, you'll only want to load the playlists for display (and not their tracks). When the user chooses the playlist, you then start loading the tracks for display.

Loading all the playlists and all their tracks at once like Guess the Intro does puts significant strain on the system and all that metadata will take a long time to load. If you really, really have to load everything at once, 1) prepare for crash reports from your users who have lots/large playlists and 2) give a much longer timeout.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top