iOS - swift 3 - DispatchGroup [英] iOS - swift 3 - DispatchGroup

查看:22
本文介绍了iOS - swift 3 - DispatchGroup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created this basic architecture to handle my networking stuff,

我想保持模块化和结构化:

i wanted to keep it modular and structured:

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

}

所以当我在单独的课堂上调用它时:

So when i call this in my seperate class:

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 添加注释.

Buuut, it seems like DispatchGroup().notify() is executed before all requests finished... because no annotations are added to mapview.

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

I already checked and annotations are loaded.

有人可以帮我设计这个架构吗?

Anybody could help me with this architecture?

谢谢和问候!

推荐答案

我认为您需要将 self.dispatchGroup.leave() 放在 Alamofire 响应处理程序中.按照书面说明,您将请求排队后立即离开.

I think you need to put self.dispatchGroup.leave() inside the Alamofire response handler. As written, you leave as soon as you queue the request.

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

这篇关于iOS - swift 3 - DispatchGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆