Realm 在登录时打开 4 次

问题描述 投票:0回答:0

我有一个应用程序可以在登录时检查用户类型,以了解导航位置。

每当我登录时,在日志中,领域将打开 4 次。

My login View:

struct LoginView: View {

    @State var userId : String = ""
    @State var password : String = ""
    @State var goToNextScreen : Int? = nil
    @State var myIsUser = false
    @State var myUser = UserInfo()
    
       
    var repo = RealmRepo()

    var body: some View {

        NavigationView{
            VStack(){
    
                TextField("Username", text: $userId)

                TextField("Password", text: $password)
                      

                Button("Login"){
  
                    repo.login(email: $userId.wrappedValue, password: $password.wrappedValue) { user, error in
                                                
                        if(user != nil){
                            
                            
                            self.repo.getUserProfile(userId: user?.id as! String).watch(block:  {items in
                                
                                self.myUser = items as! UserInfo
                                
                                                                
                                if(self.myUser.typeOfUser != true){
                                    print("this user type 1")
                                    UserDefaults.standard.set(false, forKey: "isUser")
                                    UserDefaults.standard.set(true, forKey: "isLogin")
                                    myIsUser=false
                                }else{
                                    print("this is user type2")
                                    UserDefaults.standard.set(true, forKey: "isUser")
                                    UserDefaults.standard.set(true, forKey: "isLogin")
                                    myIsUser=true
                                }
                                goToNextScreen=1
                            })
                            
                            
                        }else{
                            print("login failed")
                        }
                        
                       
                    }
                    
                }

                NavigationLink(destination: myIsUser ? AnyView(View1().navigationBarHidden(true)) : AnyView(View2().navigationBarHidden(true)) , tag: 1, selection: $goToNextScreen){
                    EmptyView()
                }
            }
        }
    }
}

只有这部分代码会打印4次日志:

INFO: [REALM] Realm opened:  /var/mobile/
这部分只是检查登录并进入视图。

这怎么可能?

是因为登录块吗? 我应该以不同的方式进行登录和 getUserProfile 调用吗?

repo.login方法:

suspend fun login(email: String, password: String): User {
        return appService.login(Credentials.emailPassword(email, password))
    }

将返回用户类型的 repo.getUserProfile():

fun getUserProfile(userId: String): CommonFlow<UserInfo?> {
        val user = realm.query<UserInfo>("_id = $0", userId).asFlow().map {
            it.list.firstOrNull()
        }.asCommonFlow()

        return user
    }
swift swiftui realm realm-mobile-platform realm-migration
© www.soinside.com 2019 - 2024. All rights reserved.