WWDC21 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: How should I force «Child-view » update after managedObjectContext changes

Forums > SwiftUI

Hello,

I have a @FetchRequest on my parent-view that segues a few child-view using NavigationLink(). My question is related to changes on thoses child-view when i add or delete object from them ( from managedObjectContext actualy). I only have a actual changes if I go back to at least on view up and come back to my view.

PrimaryView

@FetchRequest {} var myData
  ForEach(myData, id:...)data in{
    NavigationLink(destination: ChildView1(data)...
  }

ChildView1

var data : DATA

  ForEach(data, id:...)subData in{
    NavigationLink(destination: childView2(suData)...
  }

ChildView2

var subData : SUBDATA

  ForEach(subData, id:...)sata in{
    NavigationLink(destination: childView3(sata)...
  }
  .onDelete(...)

My problem would be on my ChildView2 (or even 1) if I delete (or add) an element of those list the changes don't appear immediatly. I have to go up in the view hierarchy back back down to get the changes displayed

I guest it's a simple fix, could you hel me here?

Best

Tim

   

When you pass data to the childview you need to mark the variable on the childview with

 @ObservedObject var subdata

1      

Ok that works fine, I've added a @Published property to each on my relevant classes and it seems to be working properly. I'll keep looking into it.

@Volker88 , thanks for the help.

A good explanation can be found as well here: https://www.hackingwithswift.com/books/ios-swiftui/why-state-only-works-with-structs

   

Thanks @Volker88, this helped me too.

My case was a view for editing existing CoreData. When dismissing the edit view the changes weren't being reflected until some other change or action was taken in the view.

Cheers, Dan

   

@Volker, I am trying to trigger a refresh of the parent view when I navigate back from the detailed view. I have the ObservableObject/@Published in the viewmodel and an ObservedObject in the view. However the refresh is not triggered when I navigate back to the parent view. I also manually put in objectwillchange.send() but does not help. Any pointers to solve this.

3      

Save money with our WWDC sale!

SAVE 50% To celebrate WWDC21, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.