Hello,
I am quite new to iOS programming and I am currently implementing my first iOS app with SwiftUI and CoreData. I have a question and I hope you can help me.
My app has multiple tabs and my Core Data persistence objects can be fetched, manipulated and deleted from different Views and Tabs.
I have a Detail-View that is used for multiple pages accessible from Tab 1 and Tab 2.
So, it may be the case that a user opens the Detail-View on Tab 1, then switches to the same Detail-View on Tab 2.
If now the user deletes the persistence object used for the Detail-View on one of the Tabs, I want that the Detail-View gets hidden and want to transition back to the previous List-View.
However, If I switch to my other Tab, my Detail-View now still shows my Detail-View, but without any data (I checked if my attribute is nil).
I think it may be appropriate if the Detail-View will also disappear and switch to the parent List-View for the other Tab, if the persistence object does not exists anymore.
I found that I can check a persistence objects isFault attribute, which is true, after I deleted and saved my changes via the NSManagedObjectContext.
This works in a way, that I see a Text, if my persistence object is not available anymore and otherwise shows the real Detail-View.
But thats not quite that what I want…
I tried to check if if my persistence object isFault also .onAppear and set my Bool @Binding variable to false, if my persistence object is not available anymore.
The presentView Binding is passed from my List-View into my Detail-View.
However, dismissing my view with this Bool-Binding only works for one of my Tabs and not for the other one. That is, if I delete the object from the Detail-View in Tab 1 the Detail-View gets dismissed on deletion, but if I switch to the other Tab, the Detail-View has not been dismissed.
Has anybody also struggled with issues where SwiftUI views were not updated correctly after a CoreData persistence object was deleted?
My project has grown quite big already, so I have not yet a more complete example ready. However, maybe anybody also had that issue and solved it differently.
If not, I will work on a small reproducible example in the next weeks.
Anyway, thank you for reading my question and for any support.
Best,
Bernhard
I am quite new to iOS programming and I am currently implementing my first iOS app with SwiftUI and CoreData. I have a question and I hope you can help me.
My app has multiple tabs and my Core Data persistence objects can be fetched, manipulated and deleted from different Views and Tabs.
I have a Detail-View that is used for multiple pages accessible from Tab 1 and Tab 2.
So, it may be the case that a user opens the Detail-View on Tab 1, then switches to the same Detail-View on Tab 2.
If now the user deletes the persistence object used for the Detail-View on one of the Tabs, I want that the Detail-View gets hidden and want to transition back to the previous List-View.
However, If I switch to my other Tab, my Detail-View now still shows my Detail-View, but without any data (I checked if my attribute is nil).
I think it may be appropriate if the Detail-View will also disappear and switch to the parent List-View for the other Tab, if the persistence object does not exists anymore.
I found that I can check a persistence objects isFault attribute, which is true, after I deleted and saved my changes via the NSManagedObjectContext.
Code Block var body: some View { List { if self.myPersistenceObject.isFault { // persistence Object was deleted Text(LocalizedStringKey("Persistence enity has fault")) } else { // persistence Object is alive RealDetailView() ...
This works in a way, that I see a Text, if my persistence object is not available anymore and otherwise shows the real Detail-View.
But thats not quite that what I want…
I tried to check if if my persistence object isFault also .onAppear and set my Bool @Binding variable to false, if my persistence object is not available anymore.
Code Block struct DetailView: View { @Environment(\.managedObjectContext) var managedObjectContext @ObservedObject var myPersistenceObject: MyPersistenceObject @Binding var presentView: Bool ... .onAppear { if myPersistenceObject.isFault { self.presentView = false } } struct ListViewTab1: View { @ObservedObject var myPersistenceObject: MyPersistenceObject @State var presentDetailView: Bool = false var body: some View { NavigationLink( destination: DetailView(myPersistenceObject: myPersistenceObject, presentView: $presentDetailView), isActive: $presentDetailView, label: { Text("Label") } ) } }
The presentView Binding is passed from my List-View into my Detail-View.
However, dismissing my view with this Bool-Binding only works for one of my Tabs and not for the other one. That is, if I delete the object from the Detail-View in Tab 1 the Detail-View gets dismissed on deletion, but if I switch to the other Tab, the Detail-View has not been dismissed.
Has anybody also struggled with issues where SwiftUI views were not updated correctly after a CoreData persistence object was deleted?
My project has grown quite big already, so I have not yet a more complete example ready. However, maybe anybody also had that issue and solved it differently.
If not, I will work on a small reproducible example in the next weeks.
Anyway, thank you for reading my question and for any support.
Best,
Bernhard