im having trouble setting up pagination in swift with the MovieDB API normally you would have a limit and an offet then that would relay to your model array .count -1 when working with CollectionViews Im working with a diffable datasource and cannot see the solution has anyone manage to implement this or something similar?
current API service looks like this
class APIService {
static let shared = APIService()
//always pass in your first API so the one which holds title, release date ect
func fetchMovies(completionHandler: @escaping ([Movie]?, Error?) -> ()) {
guard let url = URL(string: APINOWPLAYING) else {
print("not a valid url")
return
}
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { (data, response, error) in
if let data = data {//when Decoding use the 2nd API model with the array
if let decodedResponse = try? JSONDecoder().decode(Movies.self, from: data) {
DispatchQueue.main.async {
completionHandler(decodedResponse.results, nil)
print("TOTAL RESULTS \(decodedResponse.page)")
}
return
}
}
print("Fatch Failed \(error?.localizedDescription ?? "error unknown")")
}.resume()
}
view controller
private func setupDiffableDataSource() {
collectionView.dataSource = diffDataSource
//MARK:- SetupHeader under Compositional Sections Extension
setupHeader()
APIService.shared.fetchMovies { (movies, err) in
APIService.shared.fetchTopMovies { (movieGroup, err) in
var snapshot = self.diffDataSource.snapshot()
snapshot.appendSections([.topSection])
snapshot.appendItems(movies ?? [], toSection: .topSection)
snapshot.appendSections([.bottomSection])
let objects = movieGroup?.results ?? []
snapshot.appendItems(objects, toSection: .bottomSection)
self.diffDataSource.apply(snapshot)
}
}
}
does anyone know how to work with API for pagination?
this is what the MOVIEDB api call looks like
let APINOWPLAYING = "https://api.themoviedb.org/3/movie/now_playing?api_key=(APIKEY)&language=en-US&page=1&total_pages=56"
hoping someone can point me in the right direction
thanks