我该如何对两个网址请求使用DispatchGroup?

时间:2019-05-28 18:19:31

标签: swift dispatch-queue

我正在对两个URL请求使用DispatchGroup,但是没有得到任何数据。我在没有DispatchGroup的情况下分别尝试了每种方法,效果很好。所以问题是我没有正确使用DispatchGroup。请帮忙。

    let buildingManager =  BuildingManager()
    let anaManager = AnalyticsManager()
    let group = DispatchGroup()
    let queue = DispatchQueue.global(qos: .background)

    queue.async(group: group) {
        buildingManager.getBuildingInfos(completion: { [weak self] (buildings, success) in
            guard let self = self else {return}

            if success {
                self.buildings = buildings
                print("success")
            } else {
                print("No building infos")
            }
        })
    }

    queue.async(group: group) {
        anaManager.getAnaData(completion: {[weak self] (anaData, success) in
            guard let self = self else {return}

            if success {
                self.analyticsData = anaData
                print("success")
            } else {
                print("No analytics data")
            }
        })
    }

        group.notify(queue: DispatchQueue.main) { [weak self] in
            self?.dataTableView.reloadData()
        }

2 个答案:

答案 0 :(得分:1)

我只需要重构一下即可根据需要显式进入和离开您的小组。仅当块中的代码尚未异步时才应使用queue.async(group:)

let buildingManager =  BuildingManager()
let anaManager = AnalyticsManager()
let group = DispatchGroup()
let queue = DispatchQueue.global(qos: .background)

queue.async {
    group.enter()
    buildingManager.getBuildingInfos(completion: { [weak self] (buildings, success) in
        defer { group.leave() }

        guard let self = self else {return}

        if success {
            self.buildings = buildings
            print("success")
        } else {
            print("No building infos")
        }
    })

    group.enter()
    anaManager.getAnaData(completion: {[weak self] (anaData, success) in
        defer { group.leave() }

        guard let self = self else {return}

        if success {
            self.analyticsData = anaData
            print("success")
        } else {
            print("No analytics data")
        }
    })

    group.notify(queue: DispatchQueue.main) { [weak self] in
        self?.dataTableView.reloadData()
    }
}

答案 1 :(得分:0)

过去,我遇到了与您相同的问题,并且我在Swift中学习了更多的并行过程。 我可能是错的,但是在您的位置,我会使用OperationQueue。您可以尝试执行以下操作:

let queue = OperationQueue()

queue.addOperation {
 buildingManager.getBuildingInfos(completion: { [weak self] (buildings, success) in
            guard let self = self else {return}

            if success {
                self.buildings = buildings
                print("success")
            } else {
                print("No building infos")
            }  

            DispatchQueue.main.async {
                self?.dataTableView.reloadData()
            }
        })
}

queue.addOperation {
 anaManager.getAnaData(completion: {[weak self] (anaData, success) in
            guard let self = self else {return}

            if success {
                self.analyticsData = anaData
                print("success")
            } else {
                print("No analytics data")
            }

                DispatchQueue.main.async {
                    self?.dataTableView.reloadData()
                }
        })
}
  • 这是一种意见,而不是答案。

参考号:https://www.raywenderlich.com/5293-operation-and-operationqueue-tutorial-in-swift