Post

Replies

Boosts

Views

Activity

SwiftUI State Issue
I’m using @State to store an instance of a core data model when I tap on a button to then show that data in a new view presented in a sheet. This all seems to work when dealing with basic objects, but not for a NSManagedObject (at least I think that’s the only difference between my app code and Playground). Trying to figure out if this is a SwiftUI bug, or something I’m doing wrong. I tried reproducing in a Playground, but it’s tough because it only seems to happen when the object stored in the state property is a core data object (hard to simulate in a Playground because of all the Core Data setup). Here’s a rough example: class SomeCoreDataModel: NSManagedObject { /** ... */ } struct ContextView: View {     @State private var dataToDisplay: SomeCoreDataModel?     @State private var isDataDetailViewPresented = false     var body: some View {         VStack {             Button("Present Data 1") {                  dataToDisplay = SomeCoreDataModel(id: 1)                 isDataDetailViewPresented = true             }             Button("Present Data 2") {                  dataToDisplay = SomeCoreDataModel(id: 2)                 isDataDetailViewPresented = true             }         }         .sheet(isPresented: $isDataDetailViewPresented) {             DataDetailView(data: $dataToDisplay)         }     } } struct DataDetailView: View { @Binding var data: SomeCoreDataModel? var body: some View { /** Display data */ } } If I log to the console right after setting dataToDisplay I see the output I’d expect. However, if I inspect _self at a breakpoint at the same spot, the value of dataToDisplay property is nil. So when the new view is presented, that value is also nil (verified by setting a breakpoint in the initializer of DataDetailView. Also, after setting the value again, the correct value gets passed every time and everything seems to work as expected. Asking to see if anyone else has run into this issue or has any ideas of something I’m overlooking or not fully understanding. Also, what’s the best way to report to Apple? Is it via Feedback Assistant or bugs.swift.org?
0
0
579
Dec ’20