繁体   English   中英

登录后如何存储用户数据以在整个应用程序中使用?

[英]How do I store user data once logged in to be used throughout the app?

当用户登录时,我从 Firebase 检索他们的数据并将其存储在我的LogInViewController的结构中。 所以要从任何 ViewController 中检索它,我只需要调用LogInViewController.myVariables.person?.getFirstName()来获取他们的名字等等。但这不会造成内存使用问题,因为我每次都在实例化LogInViewController称它为? 存储要在所有 ViewController 中使用的用户数据的最佳实践是什么? 这就是我的 SceneDelegate 中的一个函数的样子。

 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        if let windowScene = scene as? UIWindowScene {
                  let window = UIWindow(windowScene: windowScene)
                  let storyboard = UIStoryboard(name: "Main", bundle: nil)
                  if KeychainWrapper.standard.string(forKey: "email") != nil && KeychainWrapper.standard.string(forKey: "password") != nil { //if the keychain isn't nil, then sign-in
                    print("here keychainwrapper is not nil")
                       Auth.auth().signIn(withEmail: KeychainWrapper.standard.string(forKey: "email")!, password:  KeychainWrapper.standard.string(forKey: "password")!) { (result, error) in
                              if error == nil {
                                   if Auth.auth().currentUser!.isEmailVerified {
                                    self.getData(result!, completionHandler: { () in
                                        //gets data, then goes to TabBarController
                                        window.setRootViewController(storyboard.instantiateViewController(withIdentifier: "TabBarController"), options: UIWindow.TransitionOptions(direction: .fade))
                                        self.window = window
                                        window.makeKeyAndVisible()
                                      })
                                   }else{ //if they aren't verified
                                    window.setRootViewController(storyboard.instantiateViewController(withIdentifier: "ViewController"), options: UIWindow.TransitionOptions(direction: .fade))
                                    self.window = window
                                    window.makeKeyAndVisible()
                                }
                          }else{ //if there is a sign-in error                                window.setRootViewController(storyboard.instantiateViewController(withIdentifier: "ViewController"), options: UIWindow.TransitionOptions(direction: .fade))
                                self.window = window
                                window.makeKeyAndVisible()
                          }
                    }
    
                  }else{ //if keychain is nil
                    window.setRootViewController(storyboard.instantiateViewController(withIdentifier: "ViewController"), options: UIWindow.TransitionOptions(direction: .fade))
                    self.window = window
                    window.makeKeyAndVisible()
            }
        
       //guard let _ = (scene as? UIWindowScene) else { return }
        }
    }

func getData(_ result:AuthDataResult, completionHandler: @escaping() -> Void){
        let user = result.user
         let docRef = db.collection("Users").document(user.uid)
           docRef.getDocument {  (document, error) in
                     if document != nil && document!.exists {
                       let d = document!.data() 
                        LogInViewController.myVariables.person = Person(d!["firstName"]! as! String, d!["lastName"]! as! String)
                        completionHandler()
                }
        }
        
    }


您可以使用 UserDefaults 存储数据以供整个应用程序使用。 如何在 Swift 中使用 UserDefaults?

请记住,不推荐用于大块数据,因为每次应用加载时数据都会加载到内存中,对于大数据,您最好使用一些本地数据库,如 Realm、Core Data 等。

暂无
暂无

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

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