Reputation: 79
I'm creating an IOS App based on cocoaLibSpotify.
In some point, I'm creating a Spotify playlist from an array of songs. I have previously obtained all the Spotify URIs and then I create the playlist and add all the tracks one by one.
The code below is in the logic class of the app and then I have a controller to show the results. The problem is that I call this logic from the controller but I dont know the way to get back when the adding process has finished. I have tried to implement a delegate but im not sure how to do it...
Which is the right way to add tracks to a playlist? I have been searching on the documentation and in the GitHub repository but I have only found an example with two nested track addings... :S
Thanks in advance! (and sorry for my english)
- (void) createPlaylist:(NSArray*)spotifyURIs withName:(NSString*)name {
int songsRead = 0;
[container createPlaylistWithName:name callback:^(SPPlaylist *createdPlaylist) {
[SPAsyncLoading waitUntilLoaded:createdPlaylist timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *loadedPlaylist, NSArray *notLoadedPlaylist) {
for (int i=[spotifyURIs count]-1; i>=0; i--) {
NSString *trackURI = spotifyURIs[i];
if (trackURI != nil){
[[SPSession sharedSession] trackForURL:[NSURL URLWithString:trackURI] callback:^(SPTrack *track) {
if (track != nil) {
[createdPlaylist addItems:[NSArray arrayWithObject: track] atIndex:[[createdPlaylist items] count] callback:nil];
}
}];
}
songsRead++;
// If I have read the whole tracklist, end of the process, returning to controller...
if (songsRead == [spotifyURIs count]){
// ...
}
}
}];
}];
}
Upvotes: 0
Views: 176
Reputation: 43
What you need is to use the KVO system on your new playlist or using BLOCKS you can implement something like this
typedef void (^spotifycompletionWithData)(id data);
-(void)addTrack:(NSString *)trackURI withCompletionBlock:(spotifycompletionWithData)CompletionBlock;
then whenever you call the function you should return the loaded SPTRACK within the completion block i hope its helping
Upvotes: 2