0

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] = []
}

I'm looking for guidance on how to structure this cache efficiently. Specifically, are there any issues or problems I could be overlooking with this approach?

Thanks in advance for your help!

5
  • what type of cache do you want to use (session or permanent)? Commented Sep 30, 2024 at 13:43
  • It would be like the example code which would be just an array or dictionary or something like that for a session usage. Commented Oct 1, 2024 at 5:33
  • For this task I would use NSCahce Commented Oct 1, 2024 at 10:27
  • NSCache decides when to evict things. Which means things needed for UI could just randomly not be there when needed Commented Oct 1, 2024 at 18:43
  • You can control that behavior, check stackoverflow.com/questions/19898540/… Commented Oct 1, 2024 at 18:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.