Last reply continued:
I adjusted your code a bit where you check the deleted objectIDs inside a Notification userInfo, because my notification used the key deleted -- so I had to use NSManagedObjectContext.NotificationKey.deletedObjects.
I wrote an extension of Notification for reusing the check in my second and third View. This is the code of the Notification extension:
import CoreData
extension Notification {
		/*Returns whether this notification is about the deletion of the given `NSManagedObject` instance*/
		func isDeletion(of managedObject: NSManagedObject) -> Bool {
				guard let deletedObjectIDs = self.deletedObjectIDs
				else {
						return false
				}
				return deletedObjectIDs.contains(managedObject.objectID)
		}
		private var deletedObjectIDs: [NSManagedObjectID]? {
				guard let deletedObjects =
								self.userInfo?[NSManagedObjectContext.NotificationKey.deletedObjects.rawValue]
								as? Set<NSManagedObject>,
							deletedObjects.count > 0
				else {
						return .none
				}
				return deletedObjects.map(\.objectID)
		}
}
What I expect to happen:
If a user navigates from View 1 to View 3 and taps on the Button "DELETE this Item", I want that my Detail-Views get dismissed. I want that View 3 and View 2 get dismissed. I want to return to my first view. This should also work when a user has open View 3 from Tab 1 and Tab 2.
What actually happens:
Dismissing my View 2 and View 3 manually by clicking on the "Dismiss" button works fine. Also the "Reset" Button on View 3 works in a way that View 3 and View 2 are dismissed and I turn back to View 1.
However, as soon as I invoke the delete and save Core Data operation, my Views are not dismissed. Instead, just my View 3 gets dismissed and I am stuck at View 2, where I even cannot manually dismiss the View 2 with the "Dismiss" Button. View 2 cannot access attributes of the deleted NSManagedObject, so instead my fallback values are used.
I also tried to use the environment variable @Environment(\.presentationMode) var presentationMode and presentationMode.wrappedValue.dismiss() instead of Bindings between my Views, but that resulted in the same unwanted behavior.
I would like that all my Child-Detail-Views get dismissed on Deletion resp. firing of Notification. It does not make sense to display View 2 if it is not possible, due to the deletion of my persistence object.
I hope that someone can help me with this issue. I am really stuck here, and I just want to get it to work as I expect it.
Thank you so much for your help! 🙏🏻☺️
Best,
Bernhard
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: