Spotify iOS SDK Swift 显示所有 (!) 播放列表 (20+) [英] Spotify iOS SDK Swift display all (!) playlists (20+)

查看:34
本文介绍了Spotify iOS SDK Swift 显示所有 (!) 播放列表 (20+)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够在 Swift 中相当轻松地获得前 20 个播放列表.

I'm able to get the first 20 playlists fairly easily in Swift.

func getPlaylists() {
    //DispatchQueue.global(qos: .userInitiated).async {

        let playListRequest = try! SPTPlaylistList.createRequestForGettingPlaylists(forUser: session.canonicalUsername, withAccessToken: session.accessToken)

        Alamofire.request(playListRequest)
            .response { response in


                let list = try! SPTPlaylistList(from: response.data, with: response.response)

                for playList in list.items  {
                    if let playlist = playList as? SPTPartialPlaylist {


                        let currentPlaylist: PlaylistItem = PlaylistItem(playlistName: playlist.name, playlistURL: playlist.uri.absoluteString)
                        self.playlists.append(currentPlaylist)
                    }
                }
                self.tableView.reloadData()
        }

到目前为止 - 很好.但这(根据文档仅提供前 20 个播放列表.

So far - so good. But this (as per documentation only delivers the first 20 playlists.

如何通过添加由类似内容嵌套的循环来获得更多(所有!)播放列表

How can I get more (all!) playlists by adding a loop that is nested by something like

 while list.hasNextPage == true {
}

重构 C 代码不成功

提前致谢!

推荐答案

您不能使用 while 循环来实现,因为网络调用是异步的.您可以改为递归请求下一页,并在完成处理程序中每次返回页面时重新加载表.

You can't do it using a while loop, because the network calls are asynchronous. You can recursively request for the next page instead, and reload the table every time a page returns in the completion handler.

假设你有以下属性

var playlists = [SPTPartialPlaylist]()

在你的 getPlaylists 函数中,你可以有这样的东西(你使用 Alamofire 的方式也可以正常工作):

In your getPlaylists function, you could have something like this (the way you have it using Alamofire also works fine):

            SPTPlaylistList.playlists(forUser: session.canonicalUsername, withAccessToken: session.accessToken, callback: { (error, response) in
                self.activityIndicator.stopAnimating()
                if let listPage = response as? SPTPlaylistList, let playlists = listPage.items as? [SPTPartialPlaylist] {
                    self.playlists = playlists    // or however you want to parse these
                    self.tableView.reloadData()
                    if listPage.hasNextPage {
                        self.getNextPlaylistPage(currentPage: listPage)
                    }
                }
            })

getNextPlaylistPage 看起来像这样:

func getNextPlaylistPage(currentPage: SPTListPage) {
    currentPage.requestNextPage(withAccessToken: AppDelegate.shared().auth?.session.accessToken, callback: { (error, response) in
        if let page = response as? SPTListPage, let playlists = page.items as? [SPTPartialPlaylist] {
            self.playlists.append(contentsOf: playlists)     // or parse these beforehand, if you need/want to
            self.tableView.reloadData()
            if page.hasNextPage {
                self.getNextPlaylistPage(currentPage: page)
            }
        }
    })
}

如果您想按照您在问题中使用的方法进行更多操作,您可以使用 Alamofire 和 SPTListPage 组合构建 getNextPlaylistPage>createRequestForNextPageWithAccessToken 函数.

If you wanted to do this more in keeping with the method you have in your question, you could build getNextPlaylistPage using a combination of Alamofire and SPTListPage's createRequestForNextPageWithAccessToken function.

(注意 SPTPlaylistListSPTListPage 的子类.)

(Note that SPTPlaylistList is a subclass of SPTListPage.)

我更新我的 SpotifyMetadata 框架也有几个月了,所以上面的一些内容可能略有变化......但总体概念保持不变.

It's also been a few months since I updated my SpotifyMetadata framework, so some of the above may have changed slightly...but the general concept remains the same.

这篇关于Spotify iOS SDK Swift 显示所有 (!) 播放列表 (20+)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆