I'm displaying a list of Spotify tracks in a table view in my application, and I'd like to download the cover art for the albums that these tracks belong to.
I'm logged in to CocoaLibSpotify, and I can play all the tracks, so that part is fine. What I can't seem to get to work is the download of the cover art. I follow the code of the SimplePlayer project, with the exception that I store the SPTrack instances in a mutable dictionary (tracksDownladingForCoverArt), since there could be many tracks downloading cover art.
So, this is what I do:
[[SPSession sharedSession] trackForURL:nativeSpotifyURL callback:^(SPTrack *track) {
if (track != nil) {
[track addObserver:self forKeyPath:@"album.cover.spotifyURL" options:0 context:NULL];
self.tracksDownloadingForCoverArt[nativeSpotifyURL] = track;
[SPAsyncLoading waitUntilLoaded:track timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *tracks, NSArray *notLoadedTracks) {
if ([tracks count] > 0) {
// I don't believe I need to do anything here
}
}];
}
}];
The observeValueForKeyPath:ofObject:change:context: is called once, with an old and new value of nil, but after that, nothing. However, if I follow SimplePlayer to the letter (i.e. have self.currentTrack instead of a dictionary) and register for the key path @"self.currentTrack.album.cover.spotifyURL", the observerValue... method is called twice, once with nil and once with an actual value.
What am I missing? Oh, and I register for the spotifyURL rather than the image, so I can cache the image. That way, when the user quits the app and returns later, not all the images have to be reloaded.