我已经开始构建一个应用程序,让用户注册并创建一个家庭。现在,我正在从登录屏幕和appDelegate(如果用户已经登录)下载用户的数据。但是,当从appDelegate下载用户数据时,我将得到nil值。
奇怪的是,我的三个视图控制器(由tab控制器控制)中的每一个都写了以下内容:

var user: User? {
 didSet {
   print(user!.name!)
 }
}

当数据最终从appDelegate下载时,将打印正确的名称。但是,在任何其他时间尝试使用用户变量都不会得到任何结果。
另外,当数据从登录屏幕下载时,它工作得很好。对此非常困惑,非常感谢您的帮助。提前谢谢。

最佳答案

我知道怎么了。与firebase无关。
下载用户数据后,在app delegate中,我尝试在tabViewController中设置viewControllers的属性。实际上,我所做的是为每个视图控制器创建一个新实例,设置其属性(这就是did set属性观察器触发的原因),但是在设置rootViewController时,我创建了一个tabController的新实例,我认为它随后创建了三个view controller的新实例。
这是appDelegate中的旧代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
if let user = FIRAuth.auth()?.currentUser {
  downloadUserData(user)
  self.window?.rootViewController = storyBoard.instantiateViewControllerWithIdentifier("Home")
} else {
  window?.rootViewController = storyBoard.instantiateViewControllerWithIdentifier("SignIn")
}

return true
}

func downloadUserData(user: FIRUser) {
let ref = FIRDatabase.database().reference()

ref.child("users").queryOrderedByKey().queryEqualToValue(user.uid).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
  for child in snapshot.children {
    if let name = child.value!["name"] as? String, let familyID = child.value!["signedInTo"] as? String {
      self.user = User(uid: user.uid, name: name)
      ref.child("families").queryOrderedByKey().queryEqualToValue(familyID).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
        for child in snapshot.children {
          if let name = child.value!["name"] as? String {
            self.family = Family(uid: familyID, name: name)

            let storyBoard = UIStoryboard(name: "Main", bundle: nil)
            let tabVC = storyBoard.instantiateViewControllerWithIdentifier("Home") as? TabBarViewController
            tabVC?.user = self.user
            tabVC?.family = self.family

            let choresNav = tabVC?.viewControllers![0] as? UINavigationController

            let choresVC = choresNav?.topViewController as? ChoresViewController

            choresVC?.user = self.user
            choresVC?.family = self.family

          }
        }
      })
    }
  }
})

}
这是appDelegate,还有一个自定义TabBarViewController:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
if let user = FIRAuth.auth()?.currentUser {
  downloadUserData(user)
} else {
  window?.rootViewController = storyBoard.instantiateViewControllerWithIdentifier("SignIn")
}
return true
}

func downloadUserData(user: FIRUser) {
let ref = FIRDatabase.database().reference()

ref.child("users").queryOrderedByKey().queryEqualToValue(user.uid).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
  for child in snapshot.children {
    if let name = child.value!["name"] as? String, let familyID = child.value!["signedInTo"] as? String {
      self.user = User(uid: user.uid, name: name)
      ref.child("families").queryOrderedByKey().queryEqualToValue(familyID).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
        for child in snapshot.children {
          if let name = child.value!["name"] as? String {
            self.family = Family(uid: familyID, name: name)

            let storyBoard = UIStoryboard(name: "Main", bundle: nil)
            let tabVC = storyBoard.instantiateViewControllerWithIdentifier("Home") as? TabBarViewController
            tabVC?.user = self.user
            tabVC?.family = self.family
            self.window?.rootViewController = tabVC
          }
        }
      })
    }
  }
})
}

tabbar控制器:
var user: User?
var family: Family?

override func viewDidLoad() {
super.viewDidLoad()
print(user?.name)
print(family?.name)

let navVC = self.viewControllers![0] as? UINavigationController
let itemsNav = self.viewControllers![1] as? UINavigationController
let leaderNav = self.viewControllers![2] as? UINavigationController

let choresVC = navVC!.topViewController as? ChoresViewController
let itemsVC = itemsNav!.topViewController as? ShoppingListViewController
let leaderVC = leaderNav!.topViewController as? LeaderBoardViewController

choresVC?.user = self.user
choresVC?.family = self.family

itemsVC!.user = self.user
itemsVC!.family = self.family

leaderVC!.user = self.user
leaderVC!.family = self.family
}

我的代码仍然不好,但它已经解决了这个问题。希望它能帮助你们中的一些人。

09-05 04:07
查看更多