onReceive not invoked in ViewModifier

Originator:jaden
Number:rdar://FB8217682 Date Originated:
Status: Resolved:
Product:SwiftUI Framework Product Version:
Classification:Incorrect/Unexpected Behavior Reproducible:
 
When onReceive is used within the body of a ViewModifier, it is not called. I've found a workaround: If I call onAppear (with no arguments) prior to calling onRecieve, it works! (Even though onAppear is documented to have no affect when called with no argument—though I'm unsure why that's an option then.)

struct BrokenModifier: ViewModifier {
    func body(content: Content) -> some View {
        content
            .onReceive(Timer.publish(every: 1, on: .main, in: .default).autoconnect()) { _ in
                print("This will never print!")
        }
    }
}

// Learned workaround from StackOverflow: https://stackoverflow.com/questions/61190398/swiftui-viewmodifier-doesnt-listen-to-onreceive-events
struct WorkaroundModifier: ViewModifier {
    func body(content: Content) -> some View {
        content
            .onAppear()
            .onReceive(Timer.publish(every: 1, on: .main, in: .default).autoconnect()) { _ in
                print("This will print, thanks to the supposed no-op onAppear.")
        }
    }
}


Expected: BrokenModifier, when used, should print every 1 second, just like WorkaroundModifier.
Actual: BrokenModifier, when used, never invokes the onReceive closure argument

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!