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()
}
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 {
}
Refactoring the C-code was not successful
Thanks in advance!
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.
Assuming you have the following property
In your
getPlaylists
function, you could have something like this (the way you have it using Alamofire also works fine):Where
getNextPlaylistPage
looks something like this: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 andSPTListPage
'screateRequestForNextPageWithAccessToken
function.(Note that
SPTPlaylistList
is a subclass ofSPTListPage
.)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.