我正在使用为我的应用程序的用户存储数据。当应用程序加载时,如果用户登录,我想获取他们的数据。我需要访问三个不同的文档,因此我创建了一个加载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)的教程
我尝试过这个堆栈溢出问题(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,我仍然很困惑。
在进入下一个操作之前,我如何让我的应用程序完全执行这三个函数中的每一个。我甚至不在乎这三个功能的执行顺序是什么,只要它们在前进之前已经完全完成了。
顺便说一句,我的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不会触发。看上去有点圆滑,但很管用。
发布于 2018-03-21 22:32:25
嵌套调用将使它们连续,这不是非常有效,将需要更长的时间来完成。一种更快的方法是像现在一样让它们同时运行,当每个操作完成时,检查它是否是最后一个完成的。
首先,向视图控制器添加一个正在等待的请求数组以及检查这些请求是否都已完成的方法:
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()
}
}这样,当所有请求都完成时,您的视图控制器将被分离,并且它们仍将同时运行。
发布于 2018-03-21 22:22:23
你可以尝试像这样嵌套电话
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)
}
}https://stackoverflow.com/questions/49417446
复制相似问题