Wednesday, December 4, 2024
HomeiOS Developmentios - State wrapper not up to date within the view

ios – State wrapper not up to date within the view


I am at present constructing an app and i have encountered issues with Firebase ant information displayed within the app.

I’ve a person ProfileView the place the identify is being displayed and another information:

struct ProfileView: View {

//var person : UserData
@ObservedObject var session : SessionStore


@State var e mail: String = ""
@State var quantity: String = ""
@State var firm: String = "MB"

var person : UserData

var physique: some View {

        
        Type {
            
            HStack(alignment: .high) {
                VStack {
                    WebImage(url: URL(string: "(person.profileImageUrl!)"))
                    // Helps choices and context, like `.delayPlaceholder` to indicate placeholder solely when error
                    .resizable() // Resizable like SwiftUI.Picture, you need to use this modifier or the view will use the picture bitmap measurement
                    .placeholder(Picture(systemName: "photograph")) // Placeholder Picture
                    // Helps ViewBuilder as properly
                    .placeholder {
                        Rectangle().foregroundColor(.clear)
                    }
                    .indicator(.exercise) // Exercise Indicator
                    .transition(.fade(period: 0.5)) // Fade Transition with period
                    .scaledToFit()
                            .body(width: 100, peak: 70)
                    
                    Textual content("(person.firstname!)")
                       // .font(Font.title)
               
                }.padding()
                
            }
            .body(maxWidth: .infinity, maxHeight: .infinity, alignment: .heart)
            .listRowInsets(EdgeInsets())
            .background(Colour(UIColor.systemGray6))
            
            Part(header: Textual content("Vartotojo informacija")) {
                //TextField("Vardas", textual content: $session.userFirstName)
                   // .disableAutocorrection(true)
                //TextField("Pavardė", textual content: $session.userLastName)
                   // .disableAutocorrection(true)
                TextField("El. pašto adresas", textual content: $e mail)
                    .disableAutocorrection(true)
                TextField("Telefono numeris", textual content: $quantity)
                    .disableAutocorrection(true)
                
            }
            
            Part(header: Textual content("Įmonės informacija")) {
                TextField("Įmonės pavadinimas", textual content: $firm)
                    .disableAutocorrection(true)
                    .disabled(true)
                    .foregroundColor(Colour(UIColor.grey))
                TextField("El. pašto adresas", textual content: $e mail)
                    .disableAutocorrection(true)
                TextField("Telefono numeris", textual content: $quantity)
                    .disableAutocorrection(true)
                
            }
        }
        
        .navigationBarItems(trailing:
                            Button("Išsaugoti") {
                                print("Profilio Informacija išsaugota!")
                                //session.updateUserProfileData(firstname: session.userFirstName, lastname: session.userLastName)
                                UIApplication.shared.endEditing()
                            })
        .onAppear{
            //self.session.initUser()
        }
    
}
}

The UserData is being transfered from the ContentView:

struct ContentView: View {
@EnvironmentObject var session : SessionStore
@State var userState = UserData()
@State non-public var functionResult = false
@State var refresh: Bool = false

func getUser(){
    if(functionResult == false){
        
        DispatchQueue.primary.async {
            session.pay attention()
            session.getUserFromUID { (fetcheduser) in
                DispatchQueue.primary.async {
                    
                    self.userState = fetcheduser
                    print("UserChanged")
                    self.functionResult = true
                    print(fetcheduser)
                    refresh.toggle()
                    
                }
            }
            
        }
        
    }
    
}

var physique: some View {
    
    VStack{
        if (session.session != nil) {
    
            if(functionResult == true){
                MainView(person: userState)
            }
            
            else {
                DelayedLaunchScreen(person: userState)
            }
            
        }
        
        else{
            HomeView()
        }
    }.onAppear{
        getUser()
    }
}
}

MainView:

struct MainView: View {

@EnvironmentObject var session : SessionStore


var person : UserData







var physique: some View {
    
    if(person.sort == "employer"){
    UIKitTabView {
        CandidateListView().tab(title: "Kandidatai", picture: "particular person.2")
        ChatsView(session: self.session).tab(title: "Žinutės", picture: "message")
        ChatsHomeView(session: self.session, person: self.person).environmentObject(session).tab(title: "Darbo skelbimai", picture: "newspaper")
        ProfileMenuView(session: self.session, person: self.person).tab(title: "Paskywra", picture: "particular person")
        //CreateJobView(person: session.getUserFrom(uid: session.uid), session: self.session).tab(title: "Paskywra", picture: "particular person")
    }
    }
    if(person.sort == "worker"){
    UIKitTabView {
        JobCardListView().environmentObject(session).tab(title: "Pradžia", picture: "newspaper")
        ChatsView(session: self.session).tab(title: "Žinutės", picture: "message")
        CreateJobView(person: self.person, session: self.session).tab(title: "TEST", picture: "particular person")
        ProfileMenuView(session: self.session, person: self.person).tab(title: "Paskyra", picture: "particular person")
        //CreateJobView(person: session.getUserFrom(uid: session.uid), session: self.session).tab(title: "Paskywra", picture: "particular person")
    }
    }
    if(person.sort == "admin"){
    UIKitTabView {
        JobCardListView().environmentObject(session).tab(title: "PINIGU SKAICIUOKLE", picture: "particular person")
        
        //CreateJobView(person: session.getUserFrom(uid: session.uid), session: self.session).tab(title: "Paskywra", picture: "particular person")
    }
    }
    
    
}
}

UserData:

struct UserData: Codable, Identifiable {
var e mail : String?
var identify : String?
var firstname : String?
var lastname : String?
var sort: String?
var uid: String?
var profileImageUrl : String?
var id : String?
var fcmToken2 : String?

func getUserUDID() -> String? {
    return uid == getUID() ? uid : uid
}



}

SessionStore FetchUserFromUID operate:

func getUserFromUID(completion: @escaping (UserData)-> ()){
    
    
    ref.baby("customers").baby(uid).observe(.worth) { (snapshot) in
        DispatchQueue.primary.async {
            guard let dictionary = snapshot.worth as? [String:AnyObject]  else { return}
            
            var person = UserData()
            
            if let e mail = dictionary["email"]{ person.e mail = e mail  as? String }
            if let identify = dictionary["name"]{ person.identify = identify  as? String }
            person.firstname = (dictionary["firstname"] as! String)
            person.lastname = (dictionary["lastname"] as! String)
            person.sort = (dictionary["type"] as! String)
            person.uid = (dictionary["uid"] as! String)
            person.profileImageUrl = (dictionary["profileImageUrl"] as! String)
            person.id = snapshot.key
            person.fcmToken2 = (dictionary["fcmToken"] as! String)
            
            completion(person)
        }
    }
}

Now as soon as i manually replace some values within the firebase RTDB the console prints the brand new person values from the ContentView getUser operate.

The worth in ProfileView shows the outdated Identify and it would not replace till the app is restarted. I am guessing this is because of the truth that the Person is a State var outlined within the ContentView, and never a ObservableObject. Is there a technique to obtain a ObservableObject performance within the UserData with out altering it into Class from Struct?

I need to obtain a stay replace performance.

Thanks for all of the solutions.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments