iOS swift如何在需要返回值的函数内等待异步任务

时间:2017-09-26 00:37:27

标签: ios swift asynchronous swift3 nsurlsessiondatatask

我正在使用swift 3.0并创建了一个返回整数数组的函数。整数数组非常具体,它们来自数据库,因此HTTP调用是异步的。这是一个功能,因为我在3个不同的控制器中使用它,所以写一次是有意义的。我的问题是在底部的return语句之后返回异步代码,因此返回nil。我在这里尝试过这个例子Waiting until the task finishes但是它不起作用主要是因为我需要返回值。这是我的代码

func ColorSwitch(label: [UILabel]) -> [Int] {

    for (index, _) in label.enumerated() {
       label[index].isHidden = true
    }

    // I need the value of this variable in the return
    // statement after the async is done
    var placeArea_id = [Int]()

    let urll:URL = URL(string:ConnectionString+"url")!

    let sessionn = URLSession.shared
    var requestt = URLRequest(url: urll)
    requestt.httpMethod = "POST"


    let group = DispatchGroup()
    group.enter()

    let parameterr = "http parameters"
    requestt.httpBody = parameterr.data(using: String.Encoding.utf8)
    let task =   sessionn.dataTask(with:requestt, completionHandler: {(data, response, error) in
        if error != nil {
            print("check check error")
        } else {
            do {

                let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:Any]
                DispatchQueue.main.async {

                    if let Profiles = parsedData?["Results"] as? [AnyObject] {
                        if placeArea_id.count >= 0 {
                            placeArea_id = [Int]()   
                        }

                        for Profiles in Profiles {

                            if let pictureS = Profiles["id"] as? Int {
                                placeArea_id.append(pictureS)
                            }
                        }
                    }
                    group.leave()
                }

            } catch let error as NSError {
                print(error)

            }
        }
    })
    task.resume()


    group.notify(queue: .main) {
   // This is getting the value however can't return it here since it 
   // expects type Void
    print(placeArea_id)

    }
   // this is nil
    return placeArea_id

}

我已经检查过,并且值正在异步代码中返回,现在只需要返回它,任何建议都会很棒。

2 个答案:

答案 0 :(得分:5)

您将需要使用闭包,或将您的功能更改为同步。

func ColorSwitch(label: [UILabel], completion:@escaping ([Int])->Void) {
    completion([1,2,3,4]) // when you want to return
}

ColorSwitch(label: [UILabel()]) { (output) in
    // output is the array of ints
    print("output: \(output)")
}

这是关于闭包http://goshdarnclosuresyntax.com/

的非常好的博客

答案 1 :(得分:4)

你不能让你的函数从该函数中的异步操作中返回一个值。那会破坏异步性的目的。为了将这些数据传回到ColorSwitch(label:)函数之外,您还需要接受一个将在完成时调用的闭包,该闭包接受[Int]作为参数。您的方法声明需要看起来像这样:

func ColorSwitch(label: [UILabel], completion: @escaping ([Int]) -> Void) -> Void {

    for (index, _) in label.enumerated() {
        label[index].isHidden = true
    }

    var placeArea_id = [Int]()

    let urll:URL = URL(string:ConnectionString+"url")!

    let sessionn = URLSession.shared
    var requestt = URLRequest(url: urll)
    requestt.httpMethod = "POST"


    let group = DispatchGroup()
    group.enter()

    let parameterr = "http parameters"
    requestt.httpBody = parameterr.data(using: String.Encoding.utf8)
    let task =   sessionn.dataTask(with:requestt, completionHandler: {(data, response, error) in
        if error != nil {
            print("check check error")
        } else {
            do {

                let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:Any]
                DispatchQueue.main.async {

                    if let Profiles = parsedData?["Results"] as? [AnyObject] {
                        if placeArea_id.count >= 0 {
                            placeArea_id = [Int]()
                        }

                        for Profiles in Profiles {

                            if let pictureS = Profiles["id"] as? Int {
                                placeArea_id.append(pictureS)
                            }

                        }
                    }
                    group.leave()
                    completion(placeArea_id) // This is effectively your "return"
                }

            } catch let error as NSError {
                print(error)

            }

        }

    })
    task.resume()
}

稍后,您可以这样称呼它:

ColorSwitch(label: []) { (ids: [Int]) in
    print(ids)
}