【发布时间】:2019-03-02 21:40:31
【问题描述】:
我有一个名为 retrieveUsers() 的函数,它在一个函数内部调用,该函数获取 Post 节点下的所有帖子。
在下面找到的代码中,我将当前 UID 保存到具有全局范围的变量中。但是每次调用这个函数时,变量(lastUID)似乎都被重置了。即使这个方法本质上是在 for 循环中调用的。
func retrieveUsers(value: Post) {
print(lastUID, "-=-=--=-=")
print(value.user.userID != lastUID, ": ", value.user.userID!, " email left old ... one -> ", lastUID)
if value.user.userID != lastUID {//Ignore this line
let ref = Database.database().reference()
if value.user.userID != nil {
let UID = value.user.userID!
print(UID, "Thoi s is the uid in the users fetch fx")
ref.child("users2").child(UID).observe(.value, with: { (snapshot) in
let email = "\(snapshot.childSnapshot(forPath: "email").value as! String)"
do {
value.user.email = email
//even with the two values being in here the same thing happens
}
self.lastUID = UID
self.lastEmail = email
})
}
} else {
print("ESC this is the same user")
value.user.email = lastEmail
}
}
出了什么问题?
【问题讨论】:
-
数据从 Firebase 异步加载。您需要确保所有需要数据的代码都在闭包内(或从那里调用)。见stackoverflow.com/questions/43823808/…、firebase.googleblog.com/2018/07/…、stackoverflow.com/questions/40904141/…
标签: ios swift firebase firebase-realtime-database