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

Why are subviews displaying old data when binding objects from an array?

Forums > Swift

Whenever a user returns to a form sub-view after saving data through a binding variable, the form displays old data from before it was edited, and I cannot understand why.

I have an array of Face objects (technically Core Data entities) declared as a State variable in a parent NavigationView. I then loop through them to create NavigationLinks like this:

@State var formDieFaces: [Face] = [] // this gets initialized akin to the below (truncated)

...

let face1 = Face(entity: Face.entity(), insertInto: nil)        
self._formDieFaces = State(initialValue: [face1])

...

ForEach (formDieFaces.indices) { curIndex in
    NavigationLink(
        destination: FaceForm(self.$formDieFaces[curIndex]))
    ) {
        FaceRowView(faceToList: self.formDieFaces[curIndex])
    }
}

(Yes, I am intentionally initializing this array of Face objects by inserting into a nil moc for temporary purposes while the user edits everything. I then iterate through that array and create new Face objects in the actual moc during the save process.)

And in FaceForm, I receive the variable and "save" it as such:

@Binding var faceToEdit: Face
@State var formFaceText: String = String()

...

// in the form
TextField("Placeholder text", text: $formFaceText)

...

// on save, do the below
self.faceToEdit.text = self.formFaceText

What's weird is that on "saving" in FaceForm, the main NavigationView does update, and in that NavigationLink (FaceRowView) everything shows the correct data. But if I click back into FaceForm thereafter, it displays old data from before the save.

I have a feeling I'm doing something phenomenally obtuse with the State and Binding variables, but I just can't for the life of me figure out what's going on. I've tried various combinations of @ObservedObject, @ObservableObject, @StateObject, etc.

Could anyone please point me in the right direction?

(To be clear, FaceRowView works and displays the correct data. FaceForm is where the problem is occurring, almost like it's remembering an old copy of the data.)

If it's helpful, here is a link to a video of the behavior: https://youtu.be/8eC-TdtFP5s

---

EDIT: for those wondering, I'm not 100% sure this is a pure, proper SwiftUI solution, but I found a hack/workaround on this page that I've implemented and seems to have "solved" the problem: https://stackoverflow.com/a/57733708.

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.