Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

2
Having trouble keeping a list of photos updated (converted from binary data)
Post Flair (click to view more posts with a particular flair)
Post Body

Hi everyone.

Been bashing my head against this problem for a couple days now and could use some input. I have an ItemDetail view which loads Core Data. My Item has a .image property which holds binary data, that I convert into images.

I followed a guide to create a PhotoPickerModel. The photos chosen from the photo library are stored in a custom class PickedMediaItems:

//Class to hold picked photos from photo picker
class PickedMediaItems: ObservableObject {
    @Published var items = [PhotoPickerModel]()

    func append(item: PhotoPickerModel) {
            items.append(item)
        }

    func deleteAll() {
         for (index, _) in items.enumerated() {
             items[index].delete()
         }

         items.removeAll()
     }
}

In my ItemDetail view:

    @ObservedObject var item: Item
    @ObservedObject var mediaItems = PickedMediaItems()

List(mediaItems.items, id: \.id) { item in
                ZStack(alignment: .topLeading) {
                    if item.mediaType == .photo {
                        Image(uiImage: item.photo ?? UIImage())
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                    }  else {

                    }

                    Image(systemName: getMediaImageName(using: item))
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 24, height: 24)
                        .padding(4)
                        .background(Color.black.opacity(0.5))
                        .foregroundColor(.white)
                }
            }.frame(height: 300)

However, I want my view to work more like this:

List(PersistenceController.shared.imagesFromCoreData(object: item.image)!.items
                 , id: \.id) { item in
                ZStack(alignment: .topLeading) {
                    if item.mediaType == .photo {
                        Image(uiImage: item.photo ?? UIImage())
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                    }  else {
                        //crash app if live photo is added
                    }

                    Image(systemName: getMediaImageName(using: item))
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 24, height: 24)
                        .padding(4)
                        .background(Color.black.opacity(0.5))
                        .foregroundColor(.white)
                }
            }.frame(height: 300)

where it loads the stored binary data and converts it to the list of stored images. I have a function imagesFromCoreData that does this.

My primary issue right now is that if I am creating the list initially using this function, I do not have a dynamic list of photos.

Ideally I would want to initially set var mediaItems = to the converted binary data, therefore I can just work with mediaItems throughout the view until I save it and convert it back to binary data. This obviously can't be done in the view, but I'm struggling to figure out how to properly achieve this. Any insight would be greatly appreciated!

Author
User Disabled User Suspended
Account Strength
0%
Disabled 9 months ago
Suspended 9 months ago
Account Age
12 years
Verified Email
Yes
Verified Flair
No
Total Karma
55
Link Karma
5,091
Comment Karma
27,325
Profile updated: 1 hour ago
Posts updated: 10 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
3 years ago