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!
Post
Replies
Boosts
Views
Activity
My app uses a temporary singleton to store CKRecords for user profiles, to prevent repeated fetching of profile pics & display usernames during a user's session.
Since iOS 18, after leaving the app in the background for an unspecified period of time & reopening it, the app has started to discard the CKAssets in those 'cached' records.
The records are still there, & the custom fields such as the display username string is still accessible. However the profile pic assets aren't?
This is the code that is displaying the profile picture, could this be something to do with some changes to how CKAssets are given file urls?
if postOwnerRecord != nil, let imageAsset = postOwnerRecord!.object(forKey: "profilePicture") as? CKAsset, let photoData = NSData(contentsOf:imageAsset.fileURL!) {
if let uiImage = UIImage(data: photoData as Data) {
let imageToUse = Image(uiImage: uiImage)
Image(uiImage: imageToUse)
}
}
Expected behavior: CKAssets should persist when resuming the app from the background.
Actual behavior: CKAssets are discarded when reopening the app, but custom fields are still accessible.
Question: Is this related to iOS 18, or am I mishandling how CKAssets are cached or their file URLs? Is there a better approach to caching these assets across app sessions? Any pointers or changes would be appreciated.
I've reviewed iOS 18 release notes but didn't find any clear references to changes with CKAsset handling. Any ideas?