我正在尝试检索用户的播放列表。我参考了来自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);
}
}
}];
}];
}];
}];
}
猜猜介绍不会尝试加载所有播放列表 - 它只是在几秒钟内完成它所能加载的内容并随之而去。
在CocoaLibSpotify中加载整个播放列表树(特别是如果您还尝试加载所有曲目)可能需要相当长的时间,尤其是在第一次运行时,因为没有缓存任何内容。
通常,对于 UI,您只需要加载播放列表进行显示(而不是其曲目)。当用户选择播放列表时,您将开始加载要显示的曲目。
像 Guess the Intro 一样一次加载所有播放列表及其所有曲目确实会给系统带来很大的压力,并且所有元数据都需要很长时间才能加载。如果您真的必须一次加载所有内容,1) 为拥有大量/大型播放列表的用户的崩溃报告做好准备,以及 2) 提供更长的超时时间。