memory leak in view struct
I'm at my wit's end here. For some reason the following code causes a memory leak and I can't figure out why. If I comment out the contents of the onEditingChanged callback in TableElement there is no leak, if I remove the data binding altogether there is no leak, and if I remove the viewModel and instead just declare mapData as a state in ContentView there is no leak, but that isn't a viable solution for my actual code. Does anyone know what's causing this memory leak? Thanks in advance
Here's my model:
class EditFuelLevelViewModel: ObservableObject {
@Published var mapData: [[Float]] = [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]
}
And here's my view:
struct ContentView: View {
private struct TableElement: View {
@Binding var data: Float
@State private var text: String
init(data: Binding<Float>) {
self._data = data
self.text = String(data.wrappedValue)
}
var body: some View {
TextField(
"Enter Value",
text: $text,
onEditingChanged: { editing in
if !editing {
data = Float(text) ?? 0
}
}
)
}
}
@StateObject var viewModel: EditFuelLevelViewModel = EditFuelLevelViewModel()
@State var text = ""
let timer = Timer.publish(every: 0.5, on: .main, in: .common).autoconnect()
var body: some View {
VStack {
Text(text)
.onReceive(timer) { test in
text = String(test.hashValue)
}
TableElement(data: $viewModel.mapData[0][0])
}
}
}
Comments
Post a Comment