Thursday, April 23, 2026
HomeiOS Developmentios - State var nil in SwiftUI with async await (CloudKit)

ios – State var nil in SwiftUI with async await (CloudKit)

[ad_1]

I have never finished a lot with the adjustments to SwiftUI concurrency with async await however I’ve gone via all of the documentation and I actually do not get what is going on on right here:

I attempt to share my CloudKit database with different customers, I carried out principally 1:1 what Apple is displaying right here within the pattern code
https://github.com/apple/sample-cloudkit-sharing/blob/important/Sharing/Views/ContentView.swift
https://github.com/apple/sample-cloudkit-sharing/blob/important/Sharing/Views/CloudSharingView.swift

that is my code

                Button("share") {
                    Job {
                            strive await shareObjects()
                    }
                }
                .sheet(isPresented: $isShowingShareView) {
                    shareView()
                }
    @State non-public var activeShare: CKShare?
    @State non-public var activeContainer: CKContainer?
    
    func shareObjects() async throws {
        
        var objects = Array(viewContext.registeredObjects)
        do {
            let (one thing, share, container) = strive await PersistenceController.shared.container.share(objects, to: nil)
            activeShare = share
            activeContainer = container
            isShowingShareView = true
        }
        catch {
            print(error)
        }
        
    }
    
    non-public func shareView() -> CloudSharingView? {
        guard let share = activeShare, let container = activeContainer else {
            print("its nil")
            return nil
        }

        return CloudSharingView(container: container, share: share)
    }

The share view ought to come up within the sheet, however it simply comes up empty as a result of share and container are nil when shareView() known as (the place it prints its nil). When setting breakpoints I see there’s information in there – then not anymore, I believe some form of difficulty the place this view simply is aware of nothing about my information however why?
I additionally added @MainActor for the strategies simply to verify its actually on the principle thread, however it’s. The share motion does work, I can see it in CloudKit Dashboard

Any concepts? Thanks!

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments