How can one avoid SwiftUI processing of views referencing a CoreData entity that has been deleted?
I've taken to 'wrapping' View body's with the following:
struct ManagedObjectDeleteWrapper<Object, Root> : View
where Object : NSManagedObject, Root : View {
var object: Object
var root: (Object) -> Root
var body: some View {
Group {
if object.isFault { EmptyView() }
else { root (object) }
}
}
}
like such:
struct GameView: View {
@EnvironmentObject private var game: Game
var body: some View {
ManagedObjectDeleteWrapper (object: game) { _ in
// Show `game` attributes and relationships
}
}
}
but this has become quite tedious. It is not as simple a wrapping the 'top-level' view (in my App, three or so TabViews
), but one must wrap multiple subviews if they also reference game
.
How can I approach this short of using game.isFault
or ManagedObjectDeleteWrapper
everywhere?
I'm cognizant that perhaps my 'view flow' is leading to this problem; is there a generic approach to view hierarchies that avoids the problem? Maybe 'only delete an object from a list view; only show the object content view a navigation link on that list view' ? My current design has three tab views showing game content and one tab showing 'competitions' (as set of games); a game is deleted from the competitions tab... and if that game is being displayed all the other tab views are invalid.