spotifyplaylists

Moving playlist to folder doesn't work


I'm having issues moving a newly created SPPlaylist to a (possibly newly created) SPPlaylistFolder.

The idea is to create a folder in the user's Spotify account, where I can add playlists generated from my application. If no such folder has been created, I'm creating a new SPPlaylistFolder and save the folder id for later use.

This is what I'm doing (I've omitted parts of the code that aren't interesting to this subject):

  1. If a folderId has been previously saved (i.e. a folder created), use that ID to load the folder instance:

    ...
    
    NSError *error = nil;
    if (folderId > 0) {
      // try to fetch folder
      folder = [[SPSession sharedSession] playlistFolderForFolderId:folderId inContainer:container];
    }
    
    if (folder == nil) {
      // create folder
      folder = [container createFolderWithName:@"My Folder" error:&error];
    
      // save a reference to the folder in an instance var
      _appFolder = [folder retain];
    
      // (also saving folder.folderId in NSUserDefaults)
    }
    
    ...
    
  2. Create an SPPlaylist: [[[SPSession sharedSession] userPlaylists] createPlaylistWithName:@"My Playlist"].

  3. Use KVO to observe the container's playlists property and get notified when the playlist has been created: [[[SPSession sharedSession] userPlaylists] addObserver:self forKeyPath:@"playlists" options:0 context:nil].

  4. Observe the playlists property and move the created playlist to my SPPlaylistFolder (containerPlaylist is the playlist I've identified as the one to move):

    ...
    // identify the index of the containerPlaylist
    NSInteger playlistIndex = [[[[SPSession sharedSession] userPlaylists] playlists] indexOfObject:containerPlaylist];
    
    // move playlist
    NSError * error = nil;
    BOOL success = [container movePlaylistOrFolderAtIndex:playlistIndex ofParent:nil toIndex:0 ofNewParent:_appFolder error:&error];
    if (success) {
      // This should be a great success. But the playlist hasn't been moved, although the error variable is nil.
    }
    
    ...
    

After these steps, both the playlist and folder have been created, but the playlist hasn't been moved. And I'm not getting any errors indicating any invalid input to the movePlaylistOrFolderAtIndex method.

Am I missing something obvious here? Or is the move functionality flawed somehow?

Note: I have also tried to use this code to move playlists that have been created previously (i.e. move all playlists named "My Playlist" to the folder).

EDIT 1: I've investigated this a bit further and actually got some moving action going on. But I had to rewrite some of the code and perform the move several times (or at a later stage). It seems like this is related to the data in SPSession not being entirely synced/up-to-date (?) since it's possible to move playlists when logging later with new session.

Is it possible that it's a syncing issue, i.e. libspotify believes that the SPPlaylistFolder is created and moves SPPlaylists to it, without it actually being created yet?


Solution

  • After having updated my code with reference to this issue on cocoalibspotify, it's working better. What I didn't realize at first was how the syncing with the Spotify service worked. It can easily take several minutes for the changes to be reflected in the Spotify desktop client, for example.