简体   繁体   English

无法使DispatchGroup在Swift中正常工作

[英]Can't get DispatchGroup to work properly in Swift

Need to wait for some loop of api calls to be finished before updating the UI, but couldn't yet find out why my DispatchGroup.notify() is getting executed before the http calls: 在更新UI之前,需要等待api调用的某些循环完成,但仍无法找出为什么在http调用之前执行DispatchGroup.notify()的原因:

override func viewDidAppear(_ animated: Bool) {

    if isFirstLoad {

        ProgressIndicator.shared.showProgressView(self.view)

        let serviceUrl = MobnerServices.service_base+"GetTrainersAround?lat=\(self.currLatitude!)&lon=\(self.currLongitude!)&radius=50"

        Alamofire.request(serviceUrl).responseJSON{ response in
            do{
                guard let responseData = response.data else{
                    print("No data received.")
                    ProgressIndicator.shared.hideProgressView()
                    return
                }

                let dispatchGroup = DispatchGroup()
                let decoder = JSONDecoder()

                let retrievedTrainers = try decoder.decode([STTrainer].self, from: responseData)

                self.trainers = retrievedTrainers
                self.isFirstLoad = false

                for trainer in self.trainers{
                    dispatchGroup.enter()

                    let getUserPictureUrl = MobnerServices.service_base+"GetUserPicture?filename=\(trainer.profilePicturePath)"

                    Alamofire.request(getUserPictureUrl).responseImage { response in
                        guard let image = response.result.value else {
                            print(response.error!.localizedDescription)
                            return
                        }

                        self.trainersPictures.append(STTrainerPicture(userId: trainer.userId, profilePicture: image))
                    }

                    dispatchGroup.leave()
                }

                dispatchGroup.wait()

                ProgressIndicator.shared.hideProgressView()
                self.tableView.reloadData()

            }
            catch{
                print(error.localizedDescription)
            }
        }

    }
}

Does anyone have some tip here? 有人在这里有小费吗? Thanks in advance! 提前致谢!

Your issue is that you are calling dispatchGroup.leave() in the wrong place. 您的问题是您在错误的位置调用了dispatchGroup.leave() It needs to be inside the completion handler of the async call. 它必须位于异步调用的完成处理程序中。

let dispatchGroup = DispatchGroup()
let decoder = JSONDecoder()

let retrievedTrainers = try decoder.decode([STTrainer].self, from: responseData)

self.trainers = retrievedTrainers
self.isFirstLoad = false

for trainer in self.trainers{
    dispatchGroup.enter()

    let getUserPictureUrl = MobnerServices.service_base+"GetUserPicture?filename=\(trainer.profilePicturePath)"

    Alamofire.request(getUserPictureUrl).responseImage { response in
        guard let image = response.result.value else {
            print(response.error!.localizedDescription)
            dispatchGroup.leave()
            return
        }

        self.trainersPictures.append(STTrainerPicture(userId: trainer.userId, profilePicture: image))
        dispatchGroup.leave()
    }
}

dispatchGroup.notify(queue: .main) {
    ProgressIndicator.shared.hideProgressView()
    self.tableView.reloadData()
}

You also need to perform the UI updates on the main queue. 您还需要在主队列上执行UI更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM