I've created a simple SwiftUI application, which is displaying images from the network in the list. All images are loaded in the background. At this point, all work as expected. However, if during the load, some State variable did trigger, all my cells get re-initialized.
Let's see the code:
struct ImageView: View {
@ObservedObject var imageLoader = ImageLoader()
var body: some View {
VStack {
if self.imageLoader.isLoaded {
DisplayImage(self.imageLoader.image)
} else {
Text("Loading...")
}
}
.onAppear {
self.imageLoader.loadImage()
}
}
}
struct TableView: View {
@State var someTrigger: Bool = false;
var body: some View {
List {
ForEach( someCollection, id: \.self) {
ImageView()
}
}
}
}
So, if 'someTrigger' is triggered while images are loading, all the cells will be re-initialized, imageLoader will be re-initialzed, .onAppear will not be called, the image won't load.
Is this expected behaviour? In that case, I just can't initialize my model with some background tasks that way?
Thanks in advance.