swiftswiftuicombine

Does the @ObservedObject debounces requests to rerender the view if many @Published properties changes in a short period?


In swiftUI I can add as many @Published properties as I wish inside an ObservableObject class and every time I change a @Published value, a "signal" is sent to the publisher objectWillChange of the class.

It means that If I receive a network request with a json and set the value of multiple @Published properties inside that class multiple messages objectWillChange.send() will be sent, and since the view responds to that publisher via @ObservedObject I have a question regarding performance,

Does the @ObservedObject debounce the request to rerender the View if many signals are sent in a short period of time? for example, when receiving a json with 10 fields I might send 10 messages to ask the view to rerender,

Or will the View actually rerender 10 times and I should be more careful in how I expose the data via @Published properties? (maybe storing a single object with all the values?)


Solution

  • Yes, it doesn't matter how many times objectWillChange is called, SwiftUI will only flag the View's body for recomputation once. Essentially it coalesces all of the notifications into one action. That is why it is willChange and not didChange.