Reputation: 4862
I have the following code. It needs the Xcode 13 beta to compile. When running on iOS 14 and tapping the "Insert data" button, it crashes with the following error:
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert section 0 but there are only 0 sections after the update'
terminating with uncaught exception of type NSException
class MyViewModel: ObservableObject {
@Published var components: [String] = []
}
struct ComponentListContent: View {
@EnvironmentObject var viewModel: MyViewModel
var body: some View {
ForEach(self.$viewModel.components, id: \.self) { $component in // This syntax requires Xcode 13
TextField("Component Name", text: $component)
}
}
}
struct MySheet: View {
@EnvironmentObject var viewModel: MyViewModel
@State private var isSheetPresented = true
@State var isEditing = false
var body: some View {
NavigationView {
List {
ComponentListContent()
}
.environment(\.editMode, .constant(self.isEditing ? EditMode.active : EditMode.inactive))
.navigationBarItems(trailing:
Button(action: { self.isEditing.toggle() }, label: {
Text(self.isEditing ? "Done" : "Edit")
})
)
.sheet(isPresented: self.$isSheetPresented) {
Button("Insert data") {
self.isSheetPresented.toggle()
self.viewModel.components = ["a", "b", "c", "d"]
}
}
.navigationBarTitle("New Analysis", displayMode: .inline)
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct ContentView: View {
var body: some View {
Text("Hello, world!")
.sheet(isPresented: .constant(true)) {
MySheet()
.environmentObject(MyViewModel())
}
}
}
If I move the body of ComponentListView
into MySheet
, the error doesn't occur. If I remove the .environment(\.editMode....
, it also doesn't occur. However I do need both of these. Is there an underlying issue that I am missing?
Upvotes: 0
Views: 543
Reputation: 88192
This seems happening because of .constant
binding.
I suggest you switching to storing EditMode
instead of Bool
, and define isEditing
with getter/setter that will modify it. You could do the same with @Binding
, but this looks a little cleaner.
@State var editMode = EditMode.inactive
var isEditing: Bool {
get {
editMode.isEditing
}
nonmutating set {
if newValue {
editMode = .active
} else {
editMode = .inactive
}
}
}
// subscribe
.environment(\.editMode, $editMode)
Upvotes: 2