View with FetchRequest doesnt update on change

Hello guys,

this is my first post to this forum and really hope that somebody can help me here.
I would highly appreciate every help!

I am writing my first app with Swift UI (never used UIKit before) which I want to publish later on.

This is also my first app which has CoreData implemented.

For example I use the following entities:

Family, Person

1 Persons can have 1 Family
1 Family can have many Persons


My App is structured as the following:

ContentView:

Contains a TabView with 2 other views in it. A Settings View and a View with a LazyVGrid.

LazyVGrid View:

This View shows a GridItem for every Family. I get the Families with the following Fetchrequest:

Code Block
@Environment(\.managedObjectContext) private var viewContext
// These are the Families from the FetchRequest
@FetchRequest(entity: Family.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \Family.created, ascending: false)]
) var families: FetchedResults<Family>


Every GridItem is linking to a "FamilyDetailView" via NavigationLink. So i pass the family as the following:

Code Block
NavigationLink(destination: FamilyDetailView(family: family).environment(\.managedObjectContext, self.viewContext), label: {Text("Family Name")

In the FamilyDetailView i get the Family with a property wrapper:

Code Block
@State var family : Family


In this FamilyDetailView is the problem i have.

Here I also have a LazyVGrid, which shows 1 NavigationLink for every Person in the Family in a GridItem . In this GridItem I also show for example the "name" of the Person.

When tapping the NavigationLink i get to the last View, the PersonDetailView. This View gets the Person which is also an entity which has a relationship to the Family Entity.

I pass it as the follow:

Code Block
NavigationLink(
destination: PersonDetailView(person: person),
label: {Text("Person")})


In the PersonDetailView I now change the name of the person and save the changed to CoreData.

The change is saved without a problem, the problem is that when I go back, using the topleading back button from the NavigationView, the Views are not updated. I have to restart the App to see the changes..

I know that the Problem has to be with passing the Data, but I cant figuring out what I did wrong.

I really appreciate everyone trying to help me!

Thank you very very much!!

This might be related to an issue I'm seeing, which I'm convinced is a bug (Apple's fault).

When creating items and editing them within the same launch, things work fine, but if you try to edit an item that already existed when the app was launched, the updates are not reflected in UI.

Here's a minimal reproduction: https://github.com/literalpie/fetchrequest-bug

I submitted feedback FB20312448

(but also, this post is almost 5 years old, so maybe it isn't related at all...)

View with FetchRequest doesnt update on change
 
 
Q