UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

3      

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

 @ObservedObject var subdata

4      

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

3      

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

3      

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

6      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.