2021年4月30日星期五

UITableView willDisplay keeps calling itself when calling the API pagination

I am using MVVM and this tableview delegate is in ViewController

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {      if indexPath.row == photosVM.albumData.photos!.photo!.count - 1 {          if photosVM.albumData.photos!.pages! >= photosVM.albumData.photos!.page! {              spinner.color = UIColor.FlickrAlbum_theme              spinner = UIActivityIndicatorView(style: .medium)              spinner.startAnimating()              spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))              tblPhotos.tableFooterView = spinner              tblPhotos.tableFooterView?.isHidden = false                  //Calling search API with search keywords                  photosVM.getSearchPhotos(pageNumber: photosVM.albumData.photos!.page! + 1, searchText: txtSearch.text!) { [self] (status) in                      lblDescription.text = txtSearch.text                      tblPhotos.reloadData()                      if status == "success" {                          lblDescription.text = txtSearch.text                          tblPhotos.reloadData()                      }                      else {                          //showing error message                          showAlert(message: status)                      }                  }              spinner.stopAnimating()              tblPhotos.tableFooterView = nil              tblPhotos.tableFooterView?.isHidden = true          }     }  

and here is my ViewModel in which I have a function of calling the API and binding data to the view controller

public func getSearchPhotos(pageNumber: Int = 1, searchText: String, completion: @escaping (String) -> Void) {      let params = GetPhotosBody()      params.format = "json"      params.apiKey = Constants.apiKey      params.nojsoncallback = 1      params.page = pageNumber      params.text = searchText      params.contentType = 1      ServerManager.getSearchPhotos(params: params) { [self] (status, data) in          if status == "success" {              if pageNumber == 1 {                  albumData = data              }              else {                  albumData.photos?.page = data?.photos?.page!                  albumData.photos?.photo?.append(contentsOf: (data!.photos!.photo!))              }              //Cache Data              DataCache.instance.write(object: albumData.toJSON() as NSCoding, forKey: CacheValue.photos)          }          completion(status)      }  }  

and this is API calling manager I am using completionblock to handle the async approach

public static func getAllPhotos(params: GetPhotosBody, completion: @escaping (String ,FlickrAlbumPhotosResponse?) -> Void) {      //creating URL      //addingPercentEncoding is used for encoding the url string to support text with spaces      let url = "\(Constants.baseURL)\(FlickrAlbumEndpoints.getPhotos.rawValue)&format=\(String(describing: params.format!))&nojsoncallback=\(String(describing: params.nojsoncallback!))&api_key=\(String(describing: params.apiKey!))&page=\(String(describing: params.page!))".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!      //creating request      var request = URLRequest(url: URL(string: url)!)      request.httpMethod = HTTPMethod.get.rawValue      request.setValue("application/json", forHTTPHeaderField: "Content-Type")      //Making request to the server to call the API      Alamofire.request(request).responseObject { (response: DataResponse<FlickrAlbumPhotosResponse>) in          //Check the response of the API          switch response.result {              case .success:                  let data = response.result.value                  completion("success",data)              case .failure(let error):                  completion(error.localizedDescription, nil)          }      }  }  

Whenever I turn off the internet its keeps showing error message again and again because it keeps calling the will display delegate.

https://stackoverflow.com/questions/67342005/uitableview-willdisplay-keeps-calling-itself-when-calling-the-api-pagination May 01, 2021 at 08:55AM

没有评论:

发表评论