As per the title.
I reported a bug to Apple and mentioned it on the Apple forums some months ago here: https://developer.apple.com/forums/thread/729197
Essentially, using the sample code below, if you observe the printouts, the class is not deallocated if multiple presentationDetents are present.
import SwiftUI
struct ContentView: View {
@State var showSheet = false
var body: some View {
Button("Show Sheet") {
showSheet = true
}
.sheet(isPresented: $showSheet) {
DetailView()
// With a single detent, the below class prints "this is denit".
.presentationDetents([.medium])
// When multiple detents are specified the detail view appears to not deinit properly.
// .presentationDetents([.medium, .large])
}
}
}
struct DetailView: View {
let deinitPrinter = DeinitPrinter()
var body: some View {
Text("foobar")
}
}
final class DeinitPrinter {
init() { print("this is init") }
deinit { print("this is deinit") }
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
In an actual use case, imagine the deinit printer is a ViewModel that would now not be deinitalised correctly.
I've raised this with Apple, FB12213332 but have since heard nothing back, I was wondering surely... I can't be the only one that's noticed this. Wondering if I'm having a brainfart and I'm actually missing something here...
Any help or suggestions would be appreciated!