当人们登录我的应用程序时,我会从数据库中获取一些数据:
func DownloadButikker() {
self.ref?.child("Stores").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let store = Store()
store.Latitude = dictionary["Latitude"]?.doubleValue
store.Longitude = dictionary["Longitude"]?.doubleValue
store.Store = dictionary["Store"] as? String
store.Status = dictionary["Status"] as? String
stores.append(store)
}
})
self.performSegue(withIdentifier: "LogInToMain", sender: nil)
}
我在所有数据加载完成之前执行segue。有没有办法让观察者完成或检查是否在进行segue之前加载了所有数据?
答案 0 :(得分:3)
快速解决方案是您需要在完成异步调用后执行segue 。
func DownloadButikker() {
self.ref?.child("Stores").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let store = Store()
store.Latitude = dictionary["Latitude"]?.doubleValue
store.Longitude = dictionary["Longitude"]?.doubleValue
store.Store = dictionary["Store"] as? String
store.Status = dictionary["Status"] as? String
stores.append(store)
}
DispatchQueue.main.async {
self.performSegue(withIdentifier: "LogInToMain", sender: nil)
}
})
}
答案 1 :(得分:1)
func DownloadButikker(completion: Bool = false) {
self.ref?.child("Stores").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let store = Store()
store.Latitude = dictionary["Latitude"]?.doubleValue
store.Longitude = dictionary["Longitude"]?.doubleValue
store.Store = dictionary["Store"] as? String
store.Status = dictionary["Status"] as? String
stores.append(store)
completion(true)
}
})
}
// In your ViewController
func myFunction() {
DownloadButikker { (hasFinished) in
if hasFinished {
self.performSegue(withIdentifier: "LogInToMain", sender: nil)
}
}
}