Can someone explain why this state property is nil despite being updated before the other state property?

I took CKShare with Zone example - https://github.com/apple/sample-cloudkit-sharing

Modified it a little bit so code looks like this:

struct ResourceView: View {
    @State private var showingShare: Bool = false
    @State private var shareView: CloudSharingView?
...


    var body: some View {
        HStack {
            Button(action: {
                Task {
                    let (share,container) = try! await shareConfiguration()
                    shareView = CloudSharingView(container: container, share: share)
                    showingShare = true
                }
            }) {
                Label("Share", systemImage: "circle")
            }
...


        .sheet(isPresented: $showingShare) {
            if let shareView = shareView {
                shareView
            } else {
                Text("No sheet to show")
            }
        }

And the first time I click on Share button I am getting "No sheet to show" despite showingShare boolean being set after shareView variable. Presumably because shareView is nil.

The second time I click on Share button it shows the sharing view.

I'm guessing that's because your try! await shareConfiguration() call is asynchronous, so although you go off and try to get the share value, it hasn't returned by the time your sheet var is flipped to show it?

That also means that your CloudSharingView() function is probably not returning what you think it's going to return.

Can someone explain why this state property is nil despite being updated before the other state property?
 
 
Q