Skip to content

Instantly share code, notes, and snippets.

@iKenndac
Created April 12, 2012 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iKenndac/2367353 to your computer and use it in GitHub Desktop.
Save iKenndac/2367353 to your computer and use it in GitHub Desktop.
Simplifying the KVO'ing of the loaded property in CocoaLibSpotify
/*
Problem:
SPPlaylistContainer asynchronously loads a list of playlists. Once it has the list,
each playlist asynchronously loads metadata such as its name and track listing.
This can be a pain to observe with KVO, and firing NSNotifications every time a
property in this huge tree changes is absurd when KVO already provides this
functionality. To augment this, I suggest the following API.
*/
/** Call the provided callback block when all passed items are loaded.
The callback block will be triggered immediately if no items are provided
or all provided items are already loaded.
@param items An array of items conforming to the `SPAsyncLoading` protocol.
@param block The block to call when all given items are loaded.
*/
+(void)waitUntilLoaded:(NSArray *)items then:(void (^)(NSArray *))block;
-(void)seachForPlaylist {
NSString *targetPlaylistName = @"My Awesome Playlist";
SPPlaylistContainer *container = [[SPSession sharedSession] userPlaylists];
[SPAsyncLoadingObserver waitUntilLoaded:[NSArray arrayWithObject:container]
then:^(NSArray *loadedItems) {
// Container is loaded - wait for playlists to load
[SPAsyncLoadingObserver waitUntilLoaded:container.flattenedPlaylists
then:^(NSArray *loadedPlaylists) {
// All playlists are loaded
NSArray *names = [loadedPlaylists valueForKey:@"name"];
if ([names containsObject:targetPlaylistName])
NSLog(@"Playlist exists!");
}];
}];
}
@aanand
Copy link

aanand commented May 22, 2012

I started playing with CocoaLibSpotify just yesterday and I'm already feeling this pain, so +1 from me. I appreciate this gist is a month old,

If you have a moment, I'd really appreciate a canonical code example for asynchronously getting the full list of playlists with metadata. I'm new to KVO, so whatever I put together will likely be horrible. Might be a good thing to stick in the README.

Thanks for CocoaLibSpotify, in any case!

@aanand
Copy link

aanand commented May 22, 2012

That was meant to say "I appreciate this gist is a month old, are you still planning to implement this API?"

@iKenndac
Copy link
Author

Hi!

Yes, the API has been implemented in the 2.0-dev branch of CocoaLibSpotify, which will be merged to master in a few days. If you check out the 2.0-dev branch, you'll find a brief example in the README and good examples of how to use it in the unit tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment