Swift-使用完成处理程序更新闭包外的全局变量

时间:2018-03-01 07:11:49

标签: swift closures completionhandler

我已经搜索了但是我无法弄清楚为什么我的全局数组变量在调用此函数时不会在全局范围内更新。我在代码中注释了一些地方,我可以打印()我想要的结果,但不能在功能之外使它可用。我知道这很简单,我不了解关闭和完成处理程序,但我需要别人看一看。感谢。

import Foundation
import PlaygroundSupport

func pdate(month: String, day: String, year: String) -> String {
    let pdate = month + "/" + day + "/" + year
    return pdate
}

struct Phenomenon {
    var phenom: String
    var date: String
    var time: String
}

struct Apsides: Codable {
    let data: [Apsdata]?
    struct Apsdata: Codable {
        let phenom: String
        let year: String
        let month: String
        let day: String
        let time: String
    }
}

var arrFromURL: [(Phenomenon)] = []  //define a global I can use later- I know, globals are evil

func doURLsessions(completionHandler: @escaping (Phenomenon?)->()) {
    let sessionApsides = URLSession(configuration: URLSessionConfiguration.default)
    if let timeurl = URL(string: "http://api.usno.navy.mil/seasons?year=2018&tz=-8&dst=true") {
        (sessionApsides.dataTask(with: timeurl, completionHandler: { (data, response, error) in
            if let error = error {
                print("Error: \(error)")
            } else if let data = data {
                let jsonDecoder = JSONDecoder()
                let response = try? jsonDecoder.decode(Apsides.self, from: data)
                if (response?.data != nil) {
                    for item in [response?.data] {
                        for p in item! {
                            switch p.phenom {
                            case "Perihelion":
                                let perihelion = Phenomenon.init(
                                    phenom: p.phenom, date: pdate(month: p.month, day: p.day, year: p.year), time: p.time)
                                completionHandler(perihelion)
                            case "Aphelion":
                                let aphelion = Phenomenon.init(
                                    phenom: p.phenom, date: pdate(month: p.month, day: p.day, year: p.year), time: p.time)
                                completionHandler(aphelion)
                            default: ()
                            }
                        }
                    }
                }
            }
            //print(arrFromURL) //This works fine, but not what I want
        })).resume()
    }
}

func chPhenom(phenom: String, date: String, time: String) {
    let p = Phenomenon.init(phenom: phenom, date: date, time: time)
    arrFromURL.append(p)
    //print(arrFromURL) //Also works fine,  but I want it's contents avilable outside this function
}

doURLsessions(completionHandler: {Phenomenon in
    chPhenom(phenom: Phenomenon!.phenom, date: Phenomenon!.date, time: Phenomenon!.time)
})

print(arrFromURL) //This just prints []- why isn't the global var available here? Is it printed before the asynch func finishes?

PlaygroundPage.current.needsIndefiniteExecution = true

1 个答案:

答案 0 :(得分:0)

函数doURLsessions中的代码是异步的 - 它不会阻止其他代码执行,因为它是一个网络请求,需要一些时间才能完成。在您完成print(arrFromURL)之前调用doURLsessions的情况下,arrFromURL仍为空。