简体   繁体   English

当我更改 EnvironmentObject 值时,应用程序冻结

[英]App freezes when I change EnvironmentObject value

I want to login my user when they click a button.我想在我的用户单击按钮时登录。 When they click the button, it calls self.auth.login() which changes loggedIn to true.当他们单击按钮时,它会调用self.auth.login()loggedIn更改为true。

When this change happens, ContentView is meant to reload with HomeView() as the body View .发生此更改时, ContentView将使用HomeView()作为主体View重新加载。 However it just crashes the app.但是,它只会使应用程序崩溃。

Here's my code:这是我的代码:

ContentView.swift ContentView.swift

struct ContentView: View {
    @EnvironmentObject var settings: UserSettings
    @EnvironmentObject var auth: UserAuth
    var body: some View {
        if (!auth.loggedIn){
            return AnyView(LoginView())
        } else {
            return AnyView(HomeView())
        }

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().environmentObject(UserAuth())
    }
}

class UserAuth: ObservableObject {
    @Published var loggedIn: Bool = false

    func login(){
        self.loggedIn = true
    }
}

struct LoginView: View {
    @EnvironmentObject var auth: UserAuth
    var body: some View {
        ZStack {
            Color.orange
                .edgesIgnoringSafeArea(.all)
            VStack {
                Image("launcher_logo").resizable()
                .scaledToFit()
                .frame(height: 100)
                    .padding(.top, 100)
                Spacer()
                Button(action: {
                    self.auth.login()
                    print("loggedIn: ", self.auth.loggedIn) // prints "loggedIn: true" then doesn't print again when pressed
                }) {
                    Text(String(auth.loggedIn))
                }

...

Any idea what the problem is?知道问题是什么吗?

EDIT:编辑:

If I change my ContentView View to just LoginView() then it doesn't freeze.如果我将我的ContentView视图更改为LoginView()那么它不会冻结。 The app works.该应用程序有效。 But I wan ContentView to be dynamic based on whether the user is loggedIn or not.但我希望 ContentView 根据用户是否登录是动态的。

EDIT2: HomeView is causing it to crash: EDIT2: HomeView 导致它崩溃:

struct HomeView: View {
//    @EnvironmentObject var auth: UserAuth
    var body: some View {
        TabView {
            HomeView()
                .tabItem {
                    Image(systemName: "1.square.fill")
                    Text(String("4"))
            }.onTapGesture {
//                self.auth.login()
            }
            HomeView()
                .tabItem {
                    Image(systemName: "3.square.fill")
                }
            Text("The Last Tab")
                .tabItem {
                    Image(systemName: "3.square.fill")
                    Text("Profile")
                }
        }
        .font(.headline)
    }

    func goOnline(){
        print("went online")
    }
}

console log:控制台日志:

[Firebase/Crashlytics][I-CLS000000] Failed to download settings Error Domain=FIRCLSNetworkError Code=-5 "(null)" UserInfo={status_code=404, type=2, request_id=, content_type=text/html; charset=utf-8}
2020-06-04 19:53:04.408869+1000 App[6718:4035694] Connection 5: received failure notification
2020-06-04 19:53:04.409001+1000 App[6718:4035694] Connection 5: failed to connect 3:-9816, reason -1
2020-06-04 19:53:04.409111+1000 App[6718:4035694] Connection 5: encountered error(3:-9816)

Your HomeView calls itself, causing an infinite loop.您的HomeView会调用自身,从而导致无限循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM