iOS Development

ios – The right way to reload a view in swiftUI

Spread the love


I am engaged on an auto login function the place upon the app opening, a cloudkit question is ran to find out if this machine has recognized person information or not and that information is loaded to my Mannequin class.

Right here is the code:

Vino App

struct vinoApp: App {
//    @StateObject non-public var dataController = DataController()
    @ObservedObject var mannequin = Mannequin()
    var physique: some Scene {
        WindowGroup {
            ContentView()
                .onAppear(carry out: {
                    Process {
                        do{
                            strive await loadActiveUser()
                        }
                        catch {
                            print("Error: (error)")
                        }
                    }
                })
        }
    }
    
    non-public func loadActiveUser() async throws {
        let report = strive await queryUserRecord()
        if report != [] {
            //this line works
            mannequin.activeUser = strive await UserObj(ckrecord: report[0])
            mannequin.userDoesntExist = false
        }
        else { mannequin.activeUser = UserObj() }
    }
    
    /**
     Queries Account Information from non-public 'AccountInformation' Document Zone
     
     What does this imply?: Passing argument of non-sendable kind 'NSPredicate' exterior of fundamental actor-isolated context might introduce information races
     */
    non-public func queryUserRecord() async throws -> [CKRecord] {
        let cloudDB = CKContainer.default().publicCloudDatabase
        let machine = UIDevice.present.identifierForVendor
        
        //set pred
        let pred = NSPredicate(format: "LastKnownDevice == %@", machine!.uuidString)
        print(machine!.uuidString)
        let question = CKQuery(recordType: "AccountInfo", predicate: pred)
        //question public report
        let (userResults, _) = strive await cloudDB.data(matching: question)
        //return the general public CKRecord of the person
        return userResults.compactMap { _, end in
            guard let report = strive? consequence.get() else { return nil }
            return report
        }
    }
}

Content material View

struct ContentView: View {
    //State vars
    @ObservedObject var mannequin = Mannequin()
    
    var physique: some View {
        VinoMainView()
        /**
         when app is opened we should consider if a person exists
         if person exists : mannequin.userDoesntExits = false
         else : mannequin.userDoesntExits = true
         */
            .fullScreenCover(isPresented: $mannequin.userDoesntExist, content material: {
                NavigationStack {
                    ZStack {
                        LoginForm()
                        NavigationLink(
                            "Dont Have an Account? Signal-Up Right here",
                            vacation spot: SignUpView(
                                userNotSignedin: $mannequin.userDoesntExist
                            )
                        )
                            .font(.customized("DMSerifDisplay-Common", dimension: 15))
                            .padding([.top], 300)
                        //Add loading view right here?
                    }
                }
            })
        }
    /**
     examine if person exists in privateDB zone
     -also units energetic person to the person discovered
     */
    func checkIfUserDoesntExists() -> Bool {
        //if the array of CKRecords returned by 'queryUserRecords()' is just not empty -> userDoesntExist = false
        if mannequin.activeUser.isEmpty() {
            //create UserObj from report[0] as a result of the non-public account zone queried from ought to solely have one report
            //MARK: have to reload views
            return true
        }
        else { return false }
    }
}

Mannequin

class Mannequin: ObservableObject {
    @Revealed var activeUser = UserObj()
    @Revealed var userDoesntExist = true
}

I do know the precise question is working as a result of once I debug it I can see the outcomes like so within the deugger:
CKRecord in Debugger

And I can see that mannequin.userDoesntExist will get modified as properly, like so:
model values

Although I can see the code is working for essentially the most half the fullScreenCover in ContentView remains to be displayed so I am questioning tips on how to refresh the ContentView to make the total display cowl go away as soon as mannequin.userDoesntExist is about to false.

Leave a Reply

Your email address will not be published. Required fields are marked *