无法检索所有用户播放列表

时间:2014-04-03 05:13:11

标签: ios spotify cocoalibspotify-2.0

我正在尝试检索用户的播放列表。我参考了来自cocoalibspotify的示例Guess The Intro,但是我只获得了2首来自星标曲目的曲目。 有什么我错过的吗?此外,api / toplists可以替代吗?我更习惯于使用json数据,所以如果有一个替代cocoalibspotify来检索用户的播放列表,我想尝试一下。

-(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);
                        }
                    }
                }];
            }];
        }];
    }];
}

1 个答案:

答案 0 :(得分:0)

猜猜简介不会尝试加载所有播放列表 - 只需几秒钟即可完成所需的内容并继续使用。

加载整个播放列表树(尤其是,如果你还试图加载所有曲目)在CocoaLibSpotify中可能需要一段时间,特别是在第一次运行时,因为没有任何缓存。

通常,对于UI,您只想加载播放列表以进行显示(而不是加载它们的曲目)。当用户选择播放列表时,您可以开始加载曲目以供显示。

一次加载所有播放列表及其所有曲目,例如Guess the Intro确实会对系统造成很大压力,所有元数据都需要很长时间才能加载。如果你真的,真的必须一次加载所有内容,1)准备好拥有大量/大型播放列表的用户的崩溃报告,2)提供更长的超时时间。