0

I want to make an API call in the tableView cell and Update a particular row after API Response.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // Here I want to call API
    // IF API call start for Cell index 0
    // After API Call 0 index cell should update.
}

I am looking best way to handle this scenario.

  • Best if this is possible without reloading tableView or tableViewCell.
  • Want to use the best of GCD and Concurrency operations.

Thank you in advance for your answers

Parth Patel
  • 915
  • 11
  • 33
  • Do you already have data for this cell and just fetching some additional data (e.g. the image)? Or do you have no data at all yet, and you’re performing a fetch request to see if there is something to retrieve or not. I.e., what do you intend to show in the cell while the request is underway (as you obviously want to do that asynchronously)? – Rob Mar 25 '22 at 21:12
  • @Rob Thank you for asking. I have already list of array which has some details based on that detail. I want to call API and from api response I will get imageURL and showing this image from this URL in tableviewCell. I am looking best way to handle this scenario. – Parth Patel Mar 27 '22 at 06:42
  • The easiest way is to use something like [Kingfisher](https://github.com/onevcat/Kingfisher), [AlamofireImage](https://github.com/Alamofire/AlamofireImage) or [SDWebImage](https://github.com/SDWebImage/SDWebImage), which each offer a `UIImageView` extension to supply a `setImage` asynchronously. To do this yourself, without one of these frameworks, is surprisingly complicated (worrying about caching, canceling prior requests to avoid getting backlogged during fast scroll, resizing asset to appropriate size, etc.), so I’d advise against that, though I’ll provide some links. – Rob Mar 27 '22 at 16:17
  • Now, all of these solutions assume you already have the URL for the image. Do you have control over this backend? Usually when we fetch the original array shown in the table view, we would include the image URL there. You really do not want to have to do one API call to get the image URL and another API call to fetch the actual image. That would be very inefficient, doubling network latency effects, resulting in a surprisingly slow UX. – Rob Mar 27 '22 at 16:29
  • @Rob I can use above mentioned third-party library when I have Image URL but I don't have Image URL. I need to call API in the tableView cell and This API will give me ImageURL. API developer is not ready to change his response. I have to manage it mySelf. – Parth Patel Mar 28 '22 at 10:53
  • Understood. Obviously, (a) it’s going to be much slower until the API developer gets around to fixing this; and (b) will unnecessarily complicate the client code. I'd personally go to the project lead and let them know that the amount of work required to work around this is likely going to be far more than just fixing the backend. – Rob Mar 28 '22 at 16:45
  • That having been said, if you're stuck with the endpoints as is, the pattern is going to be very much as described in a number of the above links, namely you will need to keep track of the `URLRequest` to fetch the URL and another to keep track of the request to fetch the image, handle cancelation if cell is reused, etc. The process will be the same (but twice as complicated). I’d suggest you take a crack at it and post another question if you get hung up. But the above links outline the various considerations. – Rob Mar 28 '22 at 16:46
  • 1
    And once you get it working, you can then add table view cell prefetching to hide some of these performance considerations from the end-user. But I would suggest deferring that until you have the basic process working correctly and make sure to test it with fast scrolling on slow network connection. – Rob Mar 28 '22 at 16:48
  • @Rob Thank you so much for your help. I will definitely improve this process. – Parth Patel Mar 29 '22 at 05:06

0 Answers0