Is it possible to lazy load images that I have stored on a local database stored as Data?
Image(uiImage: UIImage(data: realm.imageData) ?? UIImage(named: "NotFound")!)
The computing of UIImage(data: realm.imageData) makes switching to and from this tab slow.
This is user inputted images into the database so the range of images could be from 0 to a lot.
(hopefully) minimum reproducible code
struct ContentView: View {
var dataArr: [Data] = []
var body: some View {
ForEach(dataArr, id: \.self) { imageData in
Image(uiImage: UIImage(data: imageData) ?? UIImage(named: "NotFound")!)
}
}
}
The problem is that all the processing is happening on tab switch instead of on appear.
note: these images are generated on device and and there is no reference to the URL. it has to be stored as data.
ForEachinstead ofListwas causing the poor performance.var dataArr: [Data] = []is what I was referring to; it overrides Realms lazy-loading nature and ALL of the data stored in that array is loaded into memory which is likely associated with the performance issue. Use ObservedResults instead. The is no issue usingForEachwithin aList. See this example