题
我在移动新创建的项目时遇到问题 SPPlaylist
到(可能是新创建的) SPPlaylistFolder
.
这个想法是在用户的 Spotify 帐户中创建一个文件夹,我可以在其中添加从我的应用程序生成的播放列表。如果没有创建这样的文件夹,我将创建一个新的 SPPlaylistFolder
并保存文件夹 ID 供以后使用。
这就是我正在做的事情(我省略了对该主题不感兴趣的部分代码):
如果一个
folderId
之前已经保存过(即创建的文件夹),使用该 ID 加载文件夹实例:... 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) } ...
创建一个
SPPlaylist
:[[[SPSession sharedSession] userPlaylists] createPlaylistWithName:@"My Playlist"]
.使用KVO观察容器的
playlists
属性并在创建播放列表时收到通知:[[[SPSession sharedSession] userPlaylists] addObserver:self forKeyPath:@"playlists" options:0 context:nil]
.观察
playlists
属性并将创建的播放列表移动到我的SPPlaylistFolder
(containerPlaylist
是我确定要移动的播放列表):... // 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. } ...
完成这些步骤后,播放列表和文件夹都已创建,但播放列表尚未移动。我没有收到任何错误,表明有任何无效输入 movePlaylistOrFolderAtIndex
方法。
我在这里遗漏了一些明显的东西吗?或者移动功能是否存在某种缺陷?
笔记:我还尝试使用此代码来移动之前创建的播放列表(即将所有名为“我的播放列表”的播放列表移至该文件夹)。
编辑1: 我对此进行了进一步调查,并且实际上正在采取一些行动。但我必须重写一些代码并多次执行移动(或在稍后阶段)。这似乎与 SPSession 中的数据未完全同步/最新(?)有关,因为稍后使用新会话登录时可以移动播放列表。
是否有可能是同步问题,即libspotify 认为 SPPlaylistFolder
被创建并移动 SPPlaylist
就这样,还没有真正创建它?
解决方案
参考更新我的代码后 cocoalibspotify 上的这个问题, ,效果更好了。一开始我没有意识到与 Spotify 服务同步是如何工作的。例如,更改很容易需要几分钟才能反映在 Spotify 桌面客户端中。