I tried to store a list of images in CoreData, using relationship of entities.
The model are designed like this:

When add the data, I tried to code like this:
let imageData1 = UIImage(imageLiteralResourceName: "Dummy1").jpegData(compressionQuality: 1)
let imageData2 = UIImage(imageLiteralResourceName: "Dummy2").jpegData(compressionQuality: 1)
newItem.addToItemImages(NSSet(array: [imageData1!, imageData2!]))
try? self.viewContext.save()
After that, I would like to retrieve them through SwiftUI, but I failed to do so:
ForEach(Array(self.item.itemImages! as! Set), id:\.self) {image in
Image(uiImage: image)
.frame(width: 300, height: 300)
}
It complains unable to infer type of 'image' in the current context.
My question here:
what is the proper way to store sequence of images in CoreData. Is that my current approach fine? My current approach is 1 (item) to many (ItemImage) relationship. The ItemImage has a field named 'image' which is Binary Data type.
Is that my approach to store the ItemImage proper?
How can I get all the ItemImage (NSSet) from item, and then display using Image() of Swiftui?
You must first make records of type
ItemImage, and assign the image to theimageattribute of each record. Then you can use the generatedaddToItemImagesfunction to add these records to theitemImagesrelation of anItemrecord.