繁体   English   中英

无法使DispatchGroup在Swift中正常工作

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

在更新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)
            }
        }

    }
}

有人在这里有小费吗? 提前致谢!

您的问题是您在错误的位置调用了dispatchGroup.leave() 它必须位于异步调用的完成处理程序中。

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()
}

您还需要在主队列上执行UI更新。

暂无
暂无

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

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