Nate R
Nate R

Reputation: 31

SwiftUI View Code is seemingly getting called twice. What is the issue here?

I have a view that is being called using .sheet in SwiftUI. However, when this sheet is presented, I'm getting the debug "Tapped" print in console followed by this error:

Warning: Attempt to present <_TtGC7SwiftUIP13$7fff2c9bdf5c22SheetHostingControllerVS_7AnyView_: 0x7f8f1d7400f0>  on <_TtGC7SwiftUI19UIHostingControllerVVS_22_VariadicView_Children7Element_: 0x7f8f17e0dae0> which is already presenting (null)

I'm not exactly sure what is causing this error, but from what I understand it's due to the view getting called twice. I'm not sure how the view is being called twice, or even if it is, which is why I'm asking here. Below is the main view that actually houses the NavigationView in which my List is being housed view

struct AllTablesView: View {
    
    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(fetchRequest: Table.getAllTables()) var tables: FetchedResults<Table>
    
    @State private var tableTapped: Table = Table()
    
    @State private var isModal = false
    
    var body: some View {
        NavigationView {
            List {
                ForEach(self.tables) { table in
                    tableCellView(tableNumber: table.number, clean: table.clean, inUse: table.inUse)
                        .onTapGesture {
                            self.tableTapped = table
                            self.isModal = true
                            print("tapped")
                    }
                }
                .onDelete { indexSet in
                    let deletedTable = self.tables[indexSet.first!]
                    self.managedObjectContext.delete(deletedTable)
                    
                    do {
                        try self.managedObjectContext.save()
                    } catch {
                        print(error)
                    }
                }
                .sheet(isPresented: $isModal){
                    TableStatusView(table: self.tableTapped)
                        .environment(\.managedObjectContext, self.managedObjectContext)
                }
                
            }
            .navigationBarTitle("All Tables")
            .navigationBarItems(leading: EditButton(), trailing:
                HStack {
                    DeleteAllTablesButton()
                    
                    AddTableButton()
            })
        }
    }
}

And, if for whatever reason this is the issue, here is the code for my view titled "TableStatusView"

struct TableStatusView: View {
    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(fetchRequest: Table.getAllTables()) var tables: FetchedResults<Table>
    
    @State var table: Table
    
    var body: some View {
        VStack {
            Form {
                Section {
                    Text("Table \(table.number)")
                }.padding(.all, 10.0)
                
                Section {
                    Toggle(isOn: $table.inUse) {
                        Text("In Use")
                    }.padding(.all, 10.0)
                    
                    Toggle (isOn: $table.clean) {
                        Text("Clean")
                    }.padding(.all, 10.0)
                }
            }
        }
    }
}

Is there a reason I'm getting this error, or why my view is being called twice? Is it actually that it's being called twice or am I doing something wrong here? I know certain parts of my code could probably be shrunk down or written better but this is just my first dive into SwiftUI, I haven't had much experience with it outside of basic stuff. Thanks!

Upvotes: 2

Views: 1244

Answers (1)

Nate R
Nate R

Reputation: 31

I never did find a solution to the question, but I did solve the NavigationLink issue and swapped back to using that. I was using ".onTapGesture" to get a tap gesture and then generate a NavigationLink, thinking it was an action. NavigationLink, I've learned, is actually more or less a container to house content, so I replaced the .onTapGesture and TableCellView() function call with the following:

NavigationLink(destination: TableStatusView(table: table)) {
    tableCellView(tableNumber: table.number, clean: table.clean, inUse: table.inUse)
}

And fixed the issue for me. I'm still seeing some errors but from some google-fu found out that they are current bugs of SwiftUI. SwiftUI 2.0 may fix some of these issues

Upvotes: 1

Related Questions