I have a button which add a new object of Type CircleInfo to an array called circleArray! this array will be used to supply data to work for a ForEach loop, All is working except one big issue that I noticed!
The Issue: When I add new object to array, the action of updating array make ForEach to update itself as well, and it brings app and Memory to knee with a little more detailed View than a simple Circle, because it starts Re-Initializing Initialized-View again! For example we got 10 Circle in Screen, as soon as I add new Circle, Foreach will go render all rendered 10 circle plus the new one! Maybe you say it is the way of working SwiftUI which draw screen with small changes, But I am telling with more and more data it goes eat so much memory and CPU!
My Goal: I want to find a new way of adding new object to screen, more likely adding overly or something like that which those not need ForEach! because as soon as we change items of array or range of that array, it will go draw all again!
struct CircleInfo: Identifiable {
let id: UUID = UUID()
let circleColor: Color = Color(red: Double.random(in: 0...1), green: Double.random(in: 0...1), blue: Double.random(in: 0...1))
}
struct CircleView: View {
let circleInfo: CircleInfo
let index: Int
init(circleInfo: CircleInfo, index: Int) {
self.circleInfo = circleInfo
self.index = index
print("initializing circle:", index)
}
var body: some View {
Circle()
.fill(circleInfo.circleColor)
.frame(width: 200, height: 200, alignment: .center)
}
}
struct ContentView: View {
@State var circleArray: [CircleInfo] = [CircleInfo]()
var body: some View {
GeometryReader { geometry in
Color
.yellow
.ignoresSafeArea()
ForEach(Array(circleArray.enumerated()), id:\.element.id) { (index, item) in
CircleView(circleInfo: item, index: index)
.position(x: geometry.size.width/2, y: 20*CGFloat(index) + 100)
}
}
.overlay(button, alignment: Alignment.bottom)
}
var button: some View {
Button("add Circle") { circleArray.append(CircleInfo()) }
}
}
