gpt4 book ai didi

swift - 如何在 viewDidLoad() 函数上正确读取 Firebase 的数据库?

转载 作者:行者123 更新时间:2023-11-28 15:22:40 24 4
gpt4 key购买 nike

//Just a struct to save information about the User
var user = AppUser()

override func viewDidLoad() {
super.viewDidLoad()

//Verify if user is logged in
verifyUser()
user.email = "blabla"

print("viewdidload user: \(user)")

}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)

print("viewdidappear user: \(user)")

}

func verifyUser() {

print("verify user called")

//Log in verification
guard let userID = Auth.auth().currentUser?.uid else {
perform(#selector(handleLogOut), with: nil, afterDelay: 0)

print("nil user")

return
}

ref.child("users").child(userID).observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
self.user = AppUser(dictionary: dictionary)

print(self.user)
}
}) { (error) in
print(error.localizedDescription)
}
}

控制台:

 verify user called


viewdidload user: AppUser(id: nil, name: nil, email:
Optional("blabla"), completedRegister: nil, FULLUser: nil)


viewdidappear user: AppUser(id: nil, name: nil, email:
Optional("blabla"), completedRegister: nil, FULLUser: nil)


AppUser(id: nil, name: nil, email: Optional("x@gmail.com"),
completedRegister: Optional(false), FULLUser: Optional(false))

问题很简单。谁能解释一下 "print("viewDidLoad user:....")" 怎么可能在“verify user called”和“the user”之间打印数据库信息?

问题是当我尝试在 viewDidLoad 上获取用户信息时,由于某种原因该函数没有获取信息,因此值仍然为零。是时间问题吗?

我试图在函数 verifyUser() 之后放置一个循环,但它从来没有得到

out:

while user.email == nil {
print("Waiting...")
}

所以...这就是问题

谢谢!

为 Anas Menhar 编辑这是我的结构。为什么成为 NSObject 会更好?我做了一个 Struct,因为我可以做两个不同的初始化(一个是空的),而 NsObject 出于某种原因不允许我

struct AppUser {
var id: String?
var name: String?
var email: String?

var completedRegister: Bool?

var FULLUser: Bool?

init(dictionary: [String: Any]) {
self.id = dictionary["id"] as? String
self.name = dictionary["name"] as? String
self.email = dictionary["email"] as? String

self.completedRegister = Bool((dictionary["completedRegister"] as? String)!)

self.FULLUser = Bool((dictionary["FULLUser"] as? String)!)

}
init() {
self.id = nil
self.name = nil
self.email = nil

self.completedRegister = nil

self.FULLUser = nil

}
}

为 Hitesh 编辑

如果我打印字典,它会在用户完成信息的同时打印出来。在一切的尽头。

最佳答案

问题是 viewDidLoad() 在打印 "viewdidload user:\(user)" 之前调用了 verifyUser()verifyUser() 中的所有内容都将在打印之前完成(您的网络调用除外)。

所以这是为您发生的事件顺序:

  1. super.viewDidLoad()
  2. verifyUser() 被调用
  3. 打印 verifyUser() 中的语句
  4. verifyUser() 中的保护声明
  5. viewDidLoad()中打印语句
  6. super.viewDidAppear()
  7. super.viewDidAppear() 中打印

.observeSingleEventOf 的闭包内的所有内容都将在 4 之后的某个时间点发生——每当该调用结束时。如果你想在调用结束时做一些事情,把它放在闭包中。

像这样:

 ref.child("users").child(userID).observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
self.user = AppUser(dictionary: dictionary)
//***handle stuff that needs the user here
print(self.user)
} else {
//***handle getting no data for the user or data that is not [String: AnyObject]
}
}) { (error) in
//***handle any errors here
print(error.localizedDescription)
}

我添加评论的地方是在该请求完成时可能被调用的地方。

旁注:如果您要为 AppUser 使用 Struct,请确保您知道结构和类之间的区别。你可以有一个名为 AppUser 的类,并且有两个不同的 init,如下所示:

class AppUser {
var id: String?
var name: String?
var email: String?
var completedRegister: Bool?
var FULLUser: Bool?

///initialization from a dictionary
init(dictionary: [String: Any]) {
id = dictionary["id"] as? String
name = dictionary["name"] as? String
email = dictionary["email"] as? String

if let completed = dictionary["completedRegister"] as? String {
if completed == "true" {
completedRegister = true
} else if completed == "false" {
completedRegister = false
}
}

if let fullUser = dictionary["FULLUser"] as? String {
if fullUser == "true" {
FULLUser = true
} else if fullUser == "false" {
FULLUser = false
}
}
}

///initialization without a dictionary
init() {
//dont need to set any variables because they are optional
}
}

let user1 = AppUser(dictionary: ["id": "12342",
"name": "John Doe",
"email": "email@email.com",
"completedRegister": "true",
"FULLUser": "true"])
///user1 properties:
id: Optional("12342")
- some: "12342"
name: Optional("John Doe")
- some: "John Doe"
email: Optional("email@email.com")
- some: "email@email.com"
completedRegister: Optional(true)
- some: true
FULLUser: Optional(true)
- some: true

let user2 = AppUser()
///user2 properties: none

关于swift - 如何在 viewDidLoad() 函数上正确读取 Firebase 的数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45713620/

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