gpt4 book ai didi

ios - 在应用程序委托(delegate)中下载 firebase 数据

转载 作者:行者123 更新时间:2023-11-28 08:35:15 25 4
gpt4 key购买 nike

我已经开始构建一个允许用户注册和创建家庭的应用程序。现在我正在从登录屏幕为用户下载数据,如果用户已经登录,则从 appDelegate 下载数据。但是,当从 appDelegate 下载用户数据时,我得到 nil 值。

奇怪的是,我的三个 View Controller (由选项卡 Controller 控制)中的每一个都写了以下内容:

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

当数据最终从 appDelegate 下载时,打印出正确的名称。但是,在任何其他时间尝试使用用户变量都会返回 nil。

此外,当从登录屏幕下载数据时,它可以完美运行。对此非常困惑,非常感谢任何帮助。提前致谢。

最佳答案

我想出了问题所在。与火力基地无关。下载用户数据后在应用程序委托(delegate)中,我尝试在我的 tabViewController 中设置 viewControllers 的属性。我实际上做的是为每个 View Controller 创建一个新实例,设置它的属性(这就是 didSet 属性观察器被触发的原因)但是当设置 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
}
}
})
}
}
})
}

标签栏 Controller :

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
}

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

关于ios - 在应用程序委托(delegate)中下载 firebase 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37947805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com