How to cache CloudKit records efficiently in SwiftUI

I'm building a SwiftUI social photo-sharing app that uses CloudKit, where user profiles (including a CKAsset for profile pictures) are displayed throughout the app. To reduce redundant fetching of profiles across multiple views, I’m trying to implement a cache for the profile CKRecord into a custom model. (Important for handling the CKAsset for a user’s profile picture, ensuring it’s moved from the CloudKit fileURL staging area)

Here's my current approach:

struct UserProfileModel: Identifiable {
    let id: String
    let displayUsername: String
    var profilePicture: UIImage? = nil
}

class UserProfileCache: ObservableObject {
    static let shared = UserProfileCache()
    @Published var cache: [UserProfileModel] = []
}

Is this a solid approach for caching CKRecords, or is there a more efficient way to structure this for performance and memory management?

I'd appreciate any input or advice on improving this architecture for performance, memory management, and handling profile updates.

Thanks in advance for your help!

How to cache CloudKit records efficiently in SwiftUI
 
 
Q