I was just going through lots of Swift UI code and I came to know that SwiftUI redraw the view every-time when the state changes. Is is true? if yes then in that case isn't it will cause performance issue for huge complex views when even for one label change swiftUi is recreating the complete view?
CodePudding user response:
No SwiftUI doesn't draw anything. The Views are just data structs, e.g. value types, which are very cheap. When there is a state change it calls body for the affected View structs in the hierarchy, the resulting structs are diffed with the previous structs. Then only if there is any difference is actual UIKit objects are created/updated, and then those do the actual drawing on screen, if necessary, in the normal way, i.e. via CoreGraphics etc.
You can make SwiftUI more efficient if you break your body into small sub-Views, e.g. so only Views in body reference the lets/vars in that struct that might change. That is called having a tighter invalidation. SwiftUI actually enforces this on us by making body have a max of 10 View limit.