i have a strange memory leak with this code:
class Test: ObservableObject {
@Published var test:[TestStruct] = []
func getTest() {
guard let url = URL(string: "https://www.test.com") else {
return
}
URLSession.shared.dataTask(with: url) {(data,_,_) in
let testReturn = try!
JSONDecoder().decode([TestStruct].self, from: data!)
DispatchQueue.main.async {
withAnimation{
self.test = testReturn
}
}
}.resume()
}
}
struct testView: View {
@EnvironmentObject var testVar: Test
var body: some View {
VStack{
ForEach(self.testVar.test) { TestMe in
Text("print test")
}
}
}
}
when the func getTest() is called many times the forEach loop increase the memory. When the EnvironmentObject testVar is updated by a new Json call it does not create new instances but simply updates the foreach loop
I have no idea what can create this problem because the code seems to me correct. Has anyone had similar experiences?
Thanks so much