0

This is my first post to this forum and really hope that somebody can help me here. I would highly appreciate any 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 Person can have 1 Family
  • 1 Family can have many Persons

My app is structured as follows:

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:

@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:

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

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

@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:

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!!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

CoreData objects are reference types, i.e. classes, conformed to ObservableObject protocol, so instead of state wrap corresponding property with ObservedObject, like

@ObservedObject var family : Family
Asperi
  • 228,894
  • 20
  • 464
  • 690
0

Thank you very much, this helped me a lot. The FamilyDetail View is updating with that fix. Just one thing does not update. I have a "FamilyRowView" which is display for every GridItem in my Family LazyVGrid. This now also gets uses "@ObservedObject var family : Family" but still doent update. Any ideas for that? Thank you so much!

FamilyListView:

NavigationLink(destination: FamilyDetailView(family: family)
                                    .environment(\.managedObjectContext, self.viewContext),
               label: {**FamilyRowView**(family: family)
                                        .environment(\.managedObjectContext, self.viewContext)})

FamilyRowView:

struct FamilyRowView: View {
    @Environment(\.managedObjectContext) private var viewContext
    @ObservedObject var family : Family
    
    var body: some View {

It seems like to be the same as the other views that are working not but it doesnt update the View..