4

I have a SwiftUI project and a View that binds to an EnvironmentObject. This object contains a @Published property.

import Foundation

class Global : ObservableObject{

    @Published var check :Bool = false;

}

When I run the application and make changes to my property, I see that my view gets redrawn on every change.

import SwiftUI
import Combine


struct ContentView: View {

    @EnvironmentObject var global :Global

    var body: some View {
        VStack{
        VStack{
        Toggle("Checked", isOn: $global.check)
        Toggle("Checked", isOn: $global.check)
        Toggle("Checked", isOn: $global.check)
        Toggle("Checked", isOn: $global.check)
        Toggle("Checked", isOn: $global.check)
        Toggle("Checked", isOn: $global.check)


        }
        VStack{
            Toggle("Checked", isOn: $global.check)
            Toggle("Checked", isOn: $global.check)
            Toggle("Checked", isOn: $global.check)
            Toggle("Checked", isOn: $global.check)
            Toggle("Checked", isOn: $global.check)
            Toggle("Checked", isOn: $global.check)


            }
        VStack{
            Toggle("Checked", isOn: $global.check)
            Toggle("Checked", isOn: $global.check)
            Toggle("Checked", isOn: $global.check)
            Toggle("Checked", isOn: $global.check)
            Toggle("Checked", isOn: $global.check)
            Toggle("Checked", isOn: $global.check)


            }
        VStack{
            Toggle("Checked", isOn: $global.check)
            Toggle("Checked", isOn: $global.check)
            Toggle("Checked", isOn: $global.check)
            Toggle("Checked", isOn: $global.check)
            Toggle("Checked", isOn: $global.check)
            Toggle("Checked", isOn: $global.check)


            }
        }
    }
}

CPU rates get near 15%, just to draw 20 checkboxes. Is there something I am doing wrong or are there other ways to improve performance? I don't want to use a debounce to resolve this.

Extra information MacOS Catalina 10.15.2 running MacOS SwiftUI project in XCode 11.3

Attached is an Instrument profiling of me clicking and unclicking one of my checkboxes every second. In Instruments I even see CPU run op to 90%.

enter image description here

5
  • I do not observe this neither for iOS nor for macOS, ~1% in maximum. How did you test it? In which environment? More details please. Commented Dec 26, 2019 at 11:48
  • Please see my updates. I can also share my test project with you if you want Commented Dec 26, 2019 at 17:55
  • I use Xcode 11.2... too many bugs reported for 11.3 to migrate. Commented Dec 26, 2019 at 18:17
  • I tried 11.2 and got the same results. Its when you click rapidly Commented Dec 27, 2019 at 14:42
  • 1
    I've run this on the simulator and iPhone 7. CPU hit 25% on the phone when toggling rapidly. Same result if I change global to be a struct and @State. I don't believe this is unreasonable CPU usage. Commented Dec 29, 2019 at 23:26

1 Answer 1

3

SwiftUI views get redrawn when @ObservedObject, @EnvironmentObject, @StateObject or @State change. If you use so many toggles that bind to the same property, then you would expect a higher CPU usage. You aren't doing anything wrong and there is no way to improve performance of it for your purpose other than wait for improvements to SwiftUI itself.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.