Issue with onAppear being called twice on blank project

Viewed 601

On a blank project (Xcode 11.5) the onAppear method gets called twice.

struct ContentView: View {
    
    
    @State var hello : Int = 0
    
    init() {
        print("Init")
    }
    
    func appear() {
        self.hello += 1
        print(self.hello)
    }
    
    var body: some View {
        Text("Hello, World!")
            .onAppear(perform: { self.appear() })
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Expected Output:

Init
1

Actual Output:

Init
1
1

Not only this, but the hello variable doesn't increment on the second call. Its output is 1 both times. I've reproduced this bug on separate projects on two different computers. I should note that this project has both Core Data and CloudKit enabled

EDIT

This only happens in the Canvas preview. I'm getting the expected behaviour in the simulators.

0 Answers
Related