How to Save User Info to a Post using CloudKit?

1.What is the best way to link/store each User's info to a Post using CloudKit with CoreData?

  1. How can I make it that whenever the user changes their photo and I update the UserInfo record, have all the POST made by this user have the new updated photo?

.xcdataModel file has a Post entity

Post with the following attributes: caption: String, image: binary Data, postID: String, postText: String, userID: String, username: String

I use the managedobjectContext method in SwiftUI to add Posts to the database like below.


                post.username = cloudkitManager.currentUser?.name

                post.userID = cloudkitManager.userDefaultsID

                post.postText = self.viewModel.serialNumber


                post.postID = UUID().uuidString

                post.timestamp = Date()

                

                if self.viewModel.imageData() != nil {

                    post.image  = self.viewModel.imageData()

                }

                

                do {

                    if viewContext.hasChanges {

                        try viewContext.save()

                        self.viewModel.isLoading = false

                        self.presentationMode.wrappedValue.dismiss()

                    }

                } catch {

                    self.viewModel.isLoading = false

                    let nsError = error as NSError

                    self.viewModel.errorMessage = error.localizedDescription

                    self.viewModel.showErrorAlert = true

                    fatalError("Unresolved error \(nsError), \(nsError.userInfo)")

                }

But when saving the UserInfo I use a normal CloudKit container.publicCloudDatabase.save method instead of using the managedObjectContext like above

Any Advise please help 😃

How to Save User Info to a Post using CloudKit?
 
 
Q