我正在使用Google Firestore为我的应用用户存储数据。当应用加载时,如果用户已登录,我想获取他们的数据。我需要访问三个不同的文档,所以我创建了一个Loading viewController,它调用三个函数:
override func viewDidLoad() {
super.viewDidLoad()
loadingFunction1()
loadingFucntion2()
loadingFunction3()
...
self.performSegue(withIdentifier: "goToNextScreen", sender: nil)
}
每个功能看起来都像这样:
func loadingFunction1() {
self.database.collection("doc").getDocuments() { (querySnapshot, error) in
...get document from Firestore and store it locally
}
}
我需要在segue将应用程序带到下一个屏幕之前加载所有数据。
我试过了:
我已经关注了Ray Wenderlich关于DispatchGroups的教程(https://www.raywenderlich.com/148515/grand-central-dispatch-tutorial-swift-3-part-2)
我试过这个Stack Overflow问题(iOS - swift 3 - DispatchGroup)
我已阅读DispatchGroup not working in Swift 3和How do I use DispatchGroup / GCD to execute functions sequentially in swift?以及how to use a completion handler to await the completion of a firestore request,我仍然感到难过。
在继续执行下一个操作之前,如何让我的应用程序完全执行这三个功能中的每一个。我甚至不关心这三个功能的执行顺序,只要它们在继续之前完全完成即可。
BTW,我的ViewController有一个非常好的动画活动指示器视图来娱乐用户,而这一切都在发生。
使用解决方案更新
我采用了Bools建议的数组,其中包含来自评论的didSet想法:
var completedRequests: [Bool] = [false, false, false] {
didSet {
segueWhenAllRequestsAreComplete()
}
}
但是,这还不够。我必须为每个函数添加一个转义完成处理程序和一个dispatchGroup,如下所示:
func loadingFunction1(completion: @escaping (Bool) -> ()) {
DispatchQueue.global(qos: .userInteractive).async {
let downloadGroup = DispatchGroup()
var success:Bool = false
downloadGroup.enter()
self.database.collection("doc").getDocuments() { (querySnapshot, error) in
if error == nil {
...get document from Firestore and store it locally
success = true
}
downloadGroup.leave()
}
downloadGroup.wait()
DispatchQueue.main.async {
completion(success)
}
}
}
然后调用这样的函数:
DataManager.shared.loadData { success in
self.completedRequests[0] = success
}
所以现在,最后,在完成所有三个功能之前,segue不会触发。看起来有点圆,但它确实有效。
答案 0 :(得分:0)
您可以尝试嵌套这样的调用
func loadingFunction1() {
self.database.collection("doc").getDocuments() { (querySnapshot, error) in
// ...get document from Firestore and store it locally
self.loadingFunction2()
}
}
等到3
func loadingFunction3() {
self.database.collection("doc").getDocuments() { (querySnapshot, error) in
// ...get document from Firestore and store it locally
self.performSegue(withIdentifier: "goToNextScreen", sender: nil)
}
}
答案 1 :(得分:0)
嵌入调用将使它们成为顺序,这不是非常有效,并且需要更长的时间才能完成。更快的方法是让它们像你现在一样同时运行,当每一个完成时,检查它是否是最后一个完成的。
首先,添加一个待处理的请求数组,并检查它们是否已完成到视图控制器:
var completedRequests: [Bool] = [false, false, false]
func segueWhenAllRequestsCompleted() {
if !completedRequests.contains(false) {
performSegue(withIdentifier: "goToNextScreen", sender: nil)
}
}
然后,在每个加载函数中:
func loadingFunction1() {
completedRequests[0] = false
self.database.collection("doc").getDocuments() { (querySnapshot, error) in
...get document from Firestore and store it locally
self.completedRequests[0] = true
self.segueWhenAllRequestsCompleted()
}
}
func loadingFunction2() {
completedRequests[1] = false
self.database.collection("doc").getDocuments() { (querySnapshot, error) in
...get document from Firestore and store it locally
self.completedRequests[1] = true
self.segueWhenAllRequestsCompleted()
}
}
func loadingFunction3() {
completedRequests[2] = false
self.database.collection("doc").getDocuments() { (querySnapshot, error) in
...get document from Firestore and store it locally
self.completedRequests[2] = true
self.segueWhenAllRequestsCompleted()
}
}
这样,当所有请求都已完成并且它们仍将同时运行时,您的视图控制器将进行隔离。