Junior — Middle
Can you share an example of a technical improvement made to a project and describe its implementation?
sobes.tech AI
Answer from AI
In one of my iOS projects, I improved image loading performance in a list (UITableView) by implementing caching and asynchronous loading.
Implementation:
- Used
NSCacheto store already loaded images to avoid re-downloading them from the network. - In the
cellForRowAtmethod, checked if the image was in cache — if yes, displayed it immediately. - If the image was not in cache, initiated asynchronous loading via URLSession, and after loading, saved the image to cache and updated the cell.
Example code:
let imageCache = NSCache<NSString, UIImage>()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let urlString = imageURLs[indexPath.row]
if let cachedImage = imageCache.object(forKey: urlString as NSString) {
cell.imageView?.image = cachedImage
} else {
cell.imageView?.image = nil
DispatchQueue.global().async {
if let url = URL(string: urlString),
let data = try? Data(contentsOf: url),
let image = UIImage(data: data) {
self.imageCache.setObject(image, forKey: urlString as NSString)
DispatchQueue.main.async {
if let updateCell = tableView.cellForRow(at: indexPath) {
updateCell.imageView?.image = image
updateCell.setNeedsLayout()
}
}
}
}
}
return cell
}
This significantly reduced network load and improved scrolling smoothness.