【发布时间】:2019-07-31 10:59:14
【问题描述】:
为什么for循环在firebase代码之前执行,即使它是在firebase代码之后输入的?
messageDB.observe(.childAdded) { (snapshot) in
let snapshotValue = snapshot.value as! Dictionary<String,String>
print("Snapshot value \(snapshotValue)")
let email = snapshotValue["UserEmail"]!
if (email == Auth.auth().currentUser?.email as String?){
let user : UserString = UserString()
user.name = snapshotValue["UserName"]!
user.height = snapshotValue["UserHeight"]!
user.weight = snapshotValue["UserWeight"]!
user.date = snapshotValue["EntryDate"]!
userArray.append(user)
}
}
for index in 0...4{
print(index)
}
【问题讨论】:
-
因为
observe异步工作。 -
先阅读here。然后观察异步工作,所以如果你想在观察之后执行循环,你需要将它添加到块中。
-
@Kerberos 谢谢!抱歉,我对 iOS 开发真的很陌生。如何将循环添加到块中以便在之后执行?
-
@VishaalKumar 我添加了一个答案,因为它不适合评论。
标签: ios swift firebase firebase-realtime-database