PR Review Lab · Load Contact Avatars Asynchronously

25:00left of 25:00

Load Contact Avatars Asynchronously

Contacts showed a generic person icon. Now each row downloads the contact's avatar in the background and swaps it in when it arrives, so scrolling stays smooth. Failed loads are counted in metrics so we can see how often the image host has problems, and ContactCell clears its image on reuse.

AContacts/AvatarLoader.swift1 of 3

Files Changed

Contacts/

Contacts/AvatarLoader.swift

Swift · Click a line number to comment · Tap a line to comment
@@ -0,0 +1,15 @@
Added: import UIKit
Added:
Added: final class AvatarLoader {
Added: var onFailure: ((URL) -> Void)?
Added:
Added: func load(_ url: URL, completion: @escaping (UIImage) -> Void) {
Added: URLSession.shared.dataTask(with: url) { data, _, _ in
Added: guard let data, let image = UIImage(data: data) else {
Added: self.onFailure?(url)
Added: return
Added: }
Added: completion(image)
Added: }.resume()
Added: }
Added: }