TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

View not updating with @FetchRequest

Forums > SwiftUI

@Kdkwn  

I have a CoreData File that contains two entities. I'm experiencing an issue when trying to fetch items from these entities. Fetching items from the first entity works fine, but I'm encountering problems with the second entity.

In one of my views, I fetch and display the first entity in a list successfully. However, when I try to show the second entity in a different view using a @FetchRequest, the view doesn't refresh and display the data. I have to restart the app for all the data and items to appear.

Interestingly, when I have two or more items from the second entity fetched and I delete one of them, the view refreshes properly. But if I try to delete the last item, the view doesn't refresh.

Does anyone know how I can resolve this issue?

First Fetch-Request: @FetchRequest(sortDescriptors: [ NSSortDescriptor(keyPath: \FirstEntity.name, ascending: true)], predicate: NSPredicate(format: "finished == %d AND consumed == %d", true, false) ) var firstResult: FetchedResults<FirstEntity>

Second Fetch-Request: @FetchRequest(sortDescriptors: [ NSSortDescriptor(keyPath: \SecondEntity.created, ascending: true)]) var secondResult: FetchedResults<SecondEntity>

My View-Code:

@Environment(\.managedObjectContext) var managedObjectConetext

@ObservedObject var item: SecondEntity

var body: some View {
    ZStack (alignment: Alignment(horizontal: .leading, vertical: .top)) {
        HStack {
            if let data = item.image {
                let uiimage = UIImage(data: data)
                if uiimage != nil {
                    Image(uiImage: uiimage!)
                        .resizable()
                        .scaledToFill()
                        .frame(width: 70, height: 70)
                        .cornerRadius(10)
                        .padding()
                } else {
                    Image(systemName: "questionmark.app")
                        .resizable()
                        .scaledToFill()
                        .frame(width: 40, height: 40)
                        .padding()
                }
            } else {
                Image(systemName: "questionmark.app")
                    .resizable()
                    .scaledToFill()
                    .frame(width: 40, height: 40)
                    .padding()
            }

            VStack (alignment: .leading) {

                if let name = item.name {
                    Text(name)
                        .lineLimit(2)
                        .bold()
                        .foregroundColor(Color(activeColor))
                        .padding(4)
                        .padding(.top, 10)
                } else {
                    Text("No Name")
                        .padding(.top, 10)
                        .foregroundColor(Color(activeColor))
                        .padding(4)
                }

                HStack {
                    Image(systemName: "calendar.badge.exclamationmark")
                        .renderingMode(.original)
                        .font(.headline)

                    Text("•")

                    if let date = item.date {
                        Text(date, style: .date)
                    } else {
                        Text("No date")
                    }
                }
                .font(.subheadline)
                .foregroundColor(.secondary)

                HStack (spacing: 15) {
                    if item.consumed == false {
                        Button {

                        } label: {
                            ZStack {
                                ZStack {
                                    Circle()
                                        .fill(.green)
                                        .frame(width: 40, height: 40)

                                    Image(systemName: "checkmark")
                                        .font(.headline)
                                        .foregroundColor(.white)
                                }
                            }
                        }
                        .padding(.horizontal, 2)
                    }

                    if item.consumed == true {
                        Button {

                        } label: {
                            ZStack {
                                ZStack {
                                    Circle()
                                        .fill(.orange)
                                        .frame(width: 40, height: 40)

                                    Image(systemName: "gobackward")
                                        .font(.headline)
                                        .foregroundColor(.white)
                                }
                            }
                        }
                        .padding(.horizontal, 1)
                    }

                    if item.consumed == false {
                        Button {

                        } label: {
                            ZStack {
                                ZStack {
                                    Circle()
                                        .fill(.blue)
                                        .frame(width: 40, height: 40)

                                    Image(systemName: "pencil")
                                        .font(.headline)
                                        .foregroundColor(.white)
                                }
                            }
                        }
                        .padding(.horizontal, 1)
                    }

                    Button {

                    } label: {
                        ZStack {
                            ZStack {
                                Circle()
                                    .fill(.red)
                                    .frame(width: 40, height: 40)

                                Image(systemName: "trash.fill")
                                    .font(.headline)
                                    .foregroundColor(.white)
                            }
                        }
                    }
                }
                .padding(4)
                .padding(.bottom, 10)
            }

            Spacer()
        }
   }

private func deleteFoodItem() {
    DataController().deleteFoodItem(foodItem: currentFoodItem, context: managedObjectConetext)
    }

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.