[ad_1]
I have been attempting to do pagination in an effort to seek for extra outcomes however it hasn’t been working. I initially had points with not getting the identical outcomes to look once I looked for the film once more so I reset the web page quantity to 0 once I completed modifying the search bar however now I am not getting any search outcomes.
var pageNumber: Int = 1
func requestMoviesNamed(_ movieName: String) async {
var searchURLComponents = URLComponents()
searchURLComponents.scheme = "https"
searchURLComponents.host = "api.themoviedb.org"
searchURLComponents.path = "/3/search/film"
searchURLComponents.queryItems = [
URLQueryItem(name: "api_key", value: "(api)"),
URLQueryItem(name: "page", value: "(pageNumber)"),
URLQueryItem(name: "query", value: movieName)
]
guard let requestURL = searchURLComponents.url else {
print("Invalid URL.")
return
}
let urlRequest = URLRequest(url: requestURL)
do {
let (information, _) = strive await URLSession.shared.information(for: urlRequest)
DispatchQueue.important.async {
self.indicator.stopAnimating()
}
print(information)
do {
let decoder = JSONDecoder()
let movieData = strive decoder.decode(MovieData.self, from: information)
if let films = movieData.films { searchedMovies.append(contentsOf: films)
DispatchQueue.important.async {
self.tableView.reloadData()
}
if pageNumber + 1 < movieData.totalPages! {
pageNumber += 1
await requestMoviesNamed(movieName)
}
}
}
catch let error {
print(error)
}
}
catch let error {
print(error)
}
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
searchedMovies.removeAll()
tableView.reloadData()
URLSession.shared.invalidateAndCancel()
pageNumber = 0
guard let searchText = searchBar.textual content, !searchText.isEmpty else {
return
}
navigationItem.searchController?.dismiss(animated: true)
indicator.startAnimating()
Process {
await requestMoviesNamed(searchText)
}
}
[ad_2]
