SwiftUI onReceive question && Publisher bug?

Description

I have the following code in a working project, after migrating from SwiftUI1.0 to SwiftUI2.0, it behaves abnormally.

Code Block
// @Published var backgroundImage:UIImage?
XXView()
.onReceive(document.$backgroundImage) { image in
zoomToFit(image, in: geometry.size)
}


In the old SwiftUI 1.0(Xcode 11.5), this will normally work and will be triggered once when launching the View. (Because backgroundImage is nil at first and then get inited)

But in SwiftUI2.0(Xcode 12.0 beta2), this will not work and it zoomToFit will be triggered twice when launching the View.

In zoomToFit function, I printed the size of image and geometry size, here is the result

Code Block
// Xcode 11.5
Optional((284.0, 177.0))
(0.0, 0.0)
// Xcode 12.0 beta2
Optional((284.0, 177.0))
(0.0, 0.0)
Optional((284.0, 177.0))
(414.0, 647.69091796875)


Reproduce


Below is a simply way to reproduce
  • New a project

If you are creating this using Xcode 12, remember to choose lifeCycle to UIKit App Delegate(easy to work with both Xcode version)

- Add the following to ContentView.swift

Code Block
import SwiftUI
class Store: ObservableObject {
@Published var image = 2
}
struct ContentView: View {
@ObservedObject var store = Store()
var body: some View {
NavigationView {
List(0 ..< 5) { _ in
NavigationLink(
destination: Text("destination")
.onReceive(self.store.$image) { _ in
print("1")
},
label: {
Text("test")
})
}
}
}
}


And you'll find it will print "1" once in old Xcode 11.5 while print "1" twice in Xcode 12.0 beta2
SwiftUI onReceive question &amp;&amp; Publisher bug?
 
 
Q