I am working on a SwiftUI app and am having warnings first and later bugs, probably because I ignored the warnings. I want to show the warnings I am getting and I hope somebody can point out something I may be doing wrong.
Here is the relevant code:
struct CustomListView: View {
var localList:[SomeManagedObject], moc:NSManagedObjectContext
@State var showingOtherView = false
func handleCustomItem(_ argument: SomeManagedObject) {
print(#function)
self.showingOtherView.toggle()
..... Do useful things .....
}
var body: some View {
List {
ForEach(self.localList) {
item in
HStack {
Spacer()
Button(action: {
self.handleCustomItem(item)
})
{
Text(item.expression!)
.foregroundColor(Color.red))
.font(.headline)
.padding(.horizontal, 11).padding(.vertical, 15)
}.sheet(isPresented: $showingOtherView) {
OtherView()
}
Spacer()
}
}
}
}
}
This is the code for OtherView:
import SwiftUI
import CoreData
struct OtherView: View {
var body: some View {
Text("Hello OtherView")
}
}
And these are the messages I can see in the debugging console, when I click one button in the list and execute the handleCustomItem() function :
handleCustomItem(_:) 2021-04-20 22:53:10.667895+0900
TheApp[9600:5758072] [Presentation] Attempt to present
<TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView:
0x10529a510> on
<TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView:
0x10510a310> (from
<TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView:
0x10510a310>) which is already presenting
<TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView:
0x105299150>. 2021-04-20 22:53:10.668399+0900 TheApp[9600:5758072] [Presentation] Attempt to present
<TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView:
0x10529b1b0> on
<TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView:
0x10510a310> (from
<TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView:
0x10510a310>) which is already presenting
<TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView:
0x105299150>. ……….. 2021-04-20 22:53:10.670049+0900
TheApp[9600:5758072] [Presentation] Attempt to present
<TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView:
0x105118e10> on
<TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView:
0x10510a310> (from
<TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView:
0x10510a310>) which is already presenting
<TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView:
0x105299150>.
That would be great if one could see something and give me a hint on what I can do.
2
Answers
Your
.sheet(isPresented: $showingOtherView) {
is inside theForEach
. When you setshowingOtherView
to true, all the sheets in theForEach
will try to present. That’s a looooot of sheets.You want to put it outside the
ForEach
.I had the same warning message/bug, but my reason was not too many sheets as in your case. It was that I tried to present one sheet after the other too fast like this:
Which I had to change to:
And now it works perfectly. SwiftUI just needs a few milliseconds to finish the animations.