UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Switching between login and main view and calling onAppear method in SwiftUI

Forums > SwiftUI

I am trying to call a function that updates some values in the main view (DashboardView) of my app when switching to it from the SignInView after logging in. The function is inside an onAppear method that is called within the DashboardView.

Now, all this works fine when I don't have any SignInView. The app launches, onAppear is called and the values populate the view. However if I introduce the if statement that handles the switch between SignInView and DashboardView, I have to refresh the DashboardView by going to the SettingsView and back to the Dashboard, and only then the values are populated. What I don't understand is that the function within the onAppear method is called correctly, after logging in, but I need to refresh the view to actually show the data. This is my code:

struct ContentView: View {

    @ObservedObject var settingsViewModel: SettingsViewModel
    @ObservedObject var sessionManager: SessionManager

    var body: some View {
        GeometryReader {
            geometry in
                if sessionManager.session != nil && sessionManager.userAuthorization == .authorized {
                    ViewRouter(
                        settingsViewModel: settingsViewModel,
                        sessionManager: sessionManager,
                        size: geometry.size
                    )
                } else {
                    SignInView(sessionManager: sessionManager)
                }
            }.edgesIgnoringSafeArea(.vertical)
            .onAppear{
                getUser()
            }
    }
}

extension ContentView {
    func getUser() {
        sessionManager.listen()
    }
}
struct ViewRouter: View {

    @ObservedObject var settingsViewModel: SettingsViewModel
    @ObservedObject var sessionManager: SessionManager
    var size: CGSize

    var body: some View {
        VStack{
            HeaderView(headerHeight: size.height * 0.07)
            TabView{
                DashboardView(
                    viewModel: DashboardView.DashboardViewModel(
                        settingsViewModel: settingsViewModel,
                        sessionManager: sessionManager
                    ),
                    width: size.width,
                    dashboardViewHeight: size.height * 0.8
                )
                .tabItem() {
                    Image(systemName: "house.fill")
                    Text("Dashboard")
                }
                SettingsView(
                    viewModel: settingsViewModel,
                    sessionManager: sessionManager,
                    width: size.width,
                    settingsViewHeight: size.height * 0.8
                )
                .tabItem() {
                    Image(systemName: "gearshape.2.fill")
                    Text("Settings")
                }
            }.accentColor(.white)
        }
    }
}
struct DashboardView: View {

    @ObservedObject var viewModel: DashboardViewModel

    var width: CGFloat
    var dashboardViewHeight: CGFloat

    var body: some View {

            VStack(alignment: .center) {
                SubViews()
                SubViews()
                SubViews()
            }
            .onAppear {
                viewModel.updateData()
            }
    }
}

1      

@Bnerd  

In your ViewRouter call the update method here:

DashboardView(
                    viewModel: DashboardView.DashboardViewModel(
                        settingsViewModel: settingsViewModel,
                        sessionManager: sessionManager
                    ),
                    width: size.width,
                    dashboardViewHeight: size.height * 0.8
                )
                .tabItem() {
                    Image(systemName: "house.fill")
                    Text("Dashboard")
                }
                .onAppear {
                //Your update method.
                }

1      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.