iOS - swift 3 - DispatchGroup

时间:2017-01-14 20:58:56

标签: ios swift asynchronous architecture grand-central-dispatch

我创建了这个基本架构来处理我的网络内容,

我希望保持模块化和结构化:

public class NetworkManager {

    public private(set) var queue: DispatchQueue = DispatchQueue(label: "com.example.app.dispatchgroups", attributes: .concurrent, target: .main)
    public private(set) var dispatchGroup: DispatchGroup = DispatchGroup()

    private static var sharedNetworkManager: NetworkManager = {
        let networkManager = NetworkManager()
        return networkManager
    }()

    private init() {}

    class func shared() -> NetworkManager {
        return sharedNetworkManager
    }

    public func getData() {
        dispatchGroup.enter()

        queue.async(group: dispatchGroup) {
            Alamofire.request(Content.url).responseJSON { response in
                switch response.result {
                case .success(let value):
                    let json = JSON(value)
                    // do some stuff and save to Content struct
                    Content.annotations += [Station(...)]

                case .failure(let error):
                    print("error: ",error)
                }
            }

            self.dispatchGroup.leave()
        }
    }

}

struct Content {

    static var url = "url"

    static var annotations = [Station]()

}

所以当我在我的单独课堂上称呼它时:

class MainViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // some stuff ...

        NetworkManager.shared().getData()

        NetworkManager.shared().dispatchGroup.notify(queue: DispatchQueue.main) {
            self.mapView.removeAnnotations(Content.annotations)
            self.mapView.addAnnotations(Content.annotations)
        }
    }

}

Buuut,似乎DispatchGroup()。notify()在所有请求完成之前执行...因为没有注释添加到mapview。

我已经检查并加载了注释。

任何人都可以帮助我使用这种架构吗?

谢谢和问候!

2 个答案:

答案 0 :(得分:4)

我认为您需要将self.dispatchGroup.leave()放在Alamofire响应处理程序中。如上所述,您在排队请求后立即离开。

    queue.async(group: dispatchGroup) {
        Alamofire.request(Content.url).responseJSON { response in
            switch response.result {
            case .success(let value):
                let json = JSON(value)
                // do some stuff and save to Content struct
                Content.annotations += [Station(...)]

            case .failure(let error):
                print("error: ",error)
            }
            self.dispatchGroup.leave()
        }
    }

答案 1 :(得分:1)

更改您的代码,如下所示。

public func getData() {
    dispatchGroup.enter()
    queue.async(group: dispatchGroup) {
        Alamofire.request(Content.url).responseJSON { response in
            switch response.result {
            case .success(let value):
                let json = JSON(value)
                // do some stuff and save to Content struct
                Content.annotations += [Station(...)]

            case .failure(let error):
                print("error: ",error)
            }
            self.dispatchGroup.leave() // This statement has been moved
        }
    }
}

错误在于您在进入DispatchGroup后很快离开了DispatchGroup。如果必须等待网络操作完成,则应从响应处理程序中退出。