Syncronize异步功能

时间:2017-02-09 20:30:34

标签: swift swift3 grand-central-dispatch

我尝试使用swift 3创建我的第一个项目。

我尝试从我的API获取数据。如果我手动启动该功能,这非常好用。我需要同步异步请求。

我需要触发我的功能3次并等待其他人完成。

makeGetCall(URLstring: "api1")

等待完成

makeGetCall(URLstring: "api2")

等待完成

makeGetCall(URLstring: "api3") 

将此设置为背景并每5秒触发一次。

func makeGetCall(URLstring: String, update: Bool) {

    let completeURL = "http://myapi/" + URLstring


    // Set up the URL request
    guard let url = URL(string: completeURL) else {
        print("Error: cannot create URL")
        return
    }
    let urlRequest = URLRequest(url: url)

    // set up the session
    let config = URLSessionConfiguration.default
    let session = URLSession(configuration: config)

    // make the request
    let task = session.dataTask(with: urlRequest) {
        (data, response, error) in
        // check for any errors
        guard error == nil else {
            print("error calling GET on /todos/1")
            print(error as Any)
            return
        }
        // make sure we got data
        guard let responseData = data else {
            print("Error: did not receive data")
            return
        }

            // parse the result as XML
        if URLstring == "devicelist.cgi" {
            self.readDevice(XMLData: responseData)
        }

        if URLstring == "statelist.cgi" {
            self.readDeviceData(XMLData: responseData, update: update)
        }

        if URLstring == "functionlist.cgi" {
            self.readGewerke(XMLData: responseData)
        }
    }

    task.resume()

}

请有人帮忙。

哈根

这是我尝试使用完成处理程序:

    override func viewDidLoad() {
    super.viewDidLoad()

    makeGetCall(input: "statelist.cgi") {
        (result: Bool) in
        print("finished statelist")

    }
    makeGetCall(input: "devicelist.cgi") {
        (result: Bool) in
        print("finished devicelist")
    }


    makeGetCall(input: "functionlist.cgi") {
        (result: Bool) in
        print("finished functionlist")
    }

}

func makeGetCall(input: String, completion: @escaping (_ result: Bool) -> Void) {



    let completeURL = "http://192.168.0.25/addons/xmlapi/" + input


    // Set up the URL request
    guard let url = URL(string: completeURL) else {
        print("Error: cannot create URL")
        return
    }
    let urlRequest = URLRequest(url: url)

    // set up the session
    let config = URLSessionConfiguration.default
    let session = URLSession(configuration: config)

    // make the request
    let task = session.dataTask(with: urlRequest) {
        (data, response, error) in
        // check for any errors
        guard error == nil else {
            print("error calling GET on /todos/1")
            print(error as Any)
            return
        }
        // make sure we got data
        guard data != nil else {
            print("Error: did not receive data")
            return
        }
         completion(true)

    }

    task.resume()


}

如果我把3个电话放在一起就可以了。 我认为它也适用于GCD,但大多数例子都适用于swift 2。

    makeGetCall(input: "devicelist.cgi") {
        (result: Bool) in
        print("finished devicelist")
        self.makeGetCall(input: "functionlist.cgi") {
            (result: Bool) in
            print("finished functionlist")
           self.makeGetCall(input: "statelist.cgi") {
                (result: Bool) in
                print("finished statelist")

            }
        }
    }

也许现在有人可以提供帮助。

谢谢哈根

1 个答案:

答案 0 :(得分:1)

使用DispatchGroup在发生事件时收到通知。

override func viewDidLoad() {
    super.viewDidLoad()

    let stateListGroup = DispatchGroup()

    stateListGroup.enter()
    makeGetCall(input: "statelist.cgi") {
        (result: Bool) in
        print("finished statelist")
        stateListGroup.leave()
    }

    let deviceListGroup = DispatchGroup()
    deviceListGroup.enter()

    // the notify closure is called when the (stateList-) groups enter and leave counts are balanced.
    stateListGroup.notify(queue: DispatchQueue.main)  {
        self.makeGetCall(input: "devicelist.cgi") {
            (result: Bool) in
            print("finished devicelist")
            deviceListGroup.leave()
        }
    }

    let functionListGroup = DispatchGroup()
    functionListGroup.enter()

    deviceListGroup.notify(queue: DispatchQueue.main)  {
        self.makeGetCall(input: "functionList") {
            (result: Bool) in
            print("finished functionlist")
            functionListGroup.leave()
        }
    }

    functionListGroup.notify(queue: DispatchQueue.main)  {
        print("update ui here")
    }
}

打印:

statelist.cgi
finished statelist
devicelist.cgi
finished devicelist
functionList
finished functionlist
update ui here

还要记住,session.dataTask()的完成处理程序是在后台队列上调用的,所以我建议在主队列上调度completion(true)以避免意外行为:

DispatchQueue.main.async {
    completion(true)
}