@bernhard-st Whether or not the managed object is faulted is not an indication of it has been deleted or not, it is only an indication of whether the object is currently loaded into memory, so it is not best practice to rely on that for this purpose. Additionally, constantly fetching on the .onAppear is very inefficient.
There is a simple way to observe changes to the managedObjectContext through a Notification.
struct NotifyOfDeletionView: View {
		@Environment(\.managedObjectContext) var viewContext
		@Environment(\.presentationMode) var presentationMode
		@ObservedObject var managedObject: NSManagedObject
		var body: some View {
				Text(managedObject.objectID.description)
						.onReceive(
								NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave, object: viewContext),
								perform: dismissIfObjectIsDeleted(_:)
						)
		}
		private func dismissIfObjectIsDeleted(_ notification: Notification) {
let user
				if let userInfo = notification.userInfo,
					 let deletedObjectIDs = userInfo[NSManagedObjectContext.NotificationKey.deletedObjectIDs] as? Set<NSManagedObjectID>,
					 deletedObjectIDs.contains(managedObject.objectID) /* The object was deleted */
				{
						/* This will dismiss a modal (sheet/fullScreenCover) or pop the top view of a NavagationView stack */
						presentationMode.wrappedValue.dismiss()
				}
		}
}
Note that you don't need to pass in an presentView binding. The environment has a PresentationMode as shown above.
The @FetchRequest approach I described previously is still a perfectly valid approach (and it is implemented under the hood using these Notifications or something similar), but there are still use cases for using Notifications like the above. Whatever tool feels right for your use case ; )
Finally, I would discourage storing state about the view (viewAppeared) like in your most recent comment. It will inevitably lead to bugs. In SwiftUI you should always drive your views using your model. The onAppear and related modifiers allow you to respond to events, but a view shouldn't be different based on whether or not it is currently on screen.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: