gpt4 book ai didi

ios - DispatchQueue 不等待异步函数完成

转载 作者:搜寻专家 更新时间:2023-11-01 07:05:22 25 4
gpt4 key购买 nike

我正在尝试创建一个使用 USPS API 获取数据的简单包裹跟踪应用程序。 callRestService 方法成功获取数据,其完成处理程序 serviceCallback(设置 unparsedXml 属性)工作。但是,我从中调用 callRestService 的方法不会等待它及其完成处理程序完成后再继续,导致我的 print(unparsedXml) 语句返回 nil。

如下所示,我尝试使用 DispatchGroup 对象和 DispatchQueue 使函数等待 callRestService 完成,但无论如何它都会继续。如何让函数等待调用完成?

var unparsedXml:String?

public func getTrackingInfo(_ trackingNumber: String) -> TrackingInfo {
let group = DispatchGroup()
group.enter()
DispatchQueue.global(qos: DispatchQoS.default.qosClass).async {
self.callRestService(requestUrl: self.getRequest(trackingNumber))
group.leave()
}
group.wait()
print(unparsedXml)
return TrackingInfo()
}

private func getRequest(_ trackingNumber: String) -> String {
let APIUsername = "Intentionally Omitted"
let trackingXmlLink = "http://production.shippingapis.com/ShippingAPI.dll?API=TrackV2&XML=%3CTrackFieldRequest%20USERID=%22" + APIUsername + "%22%3E%20%3CRevision%3E1%3C/Revision%3E%20%3CClientIp%3E127.0.0.1%3C/ClientIp%3E%20%3C/SourceId%3E%20%3CTrackID%20ID=%22" + trackingNumber + "%22%3E%20%3CDestinationZipCode%3E66666%3C/DestinationZipCode%3E%20%3CMailingDate%3E2010-01-01%3C/MailingDate%3E%20%3C/TrackID%3E%20%3C/TrackFieldRequest%3E"
return trackingXmlLink
}

public func callRestService(requestUrl:String) ->Void
{
var request = URLRequest(url: URL(string: requestUrl)!)
request.httpMethod = "GET"

let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: serviceCallback)

task.resume()
}

private func serviceCallback(data:Data? , response:URLResponse? , error:Error? ) -> Void
{
unparsedXml = String(data: data!, encoding: .utf8)
//print(unparsedXml) Works correctly when uncommented
}

最佳答案

您的问题是 callRestService 将调度一个异步网络操作,因此您的 group.leave 将立即被调用,触发您的 group.notify.

您可以将 group.leave 放在完成处理程序中,但您应该避免阻塞代码。我建议您将 getTrackingInfo 构造为采用完成处理程序的异步函数:

public func getTrackingInfo(_ trackingNumber: String, completion:(TrackingInfo?,Error?) -> Void) {
self.callRestService(requestUrl: self.getRequest(trackingNumber)) { (data, response, error) in
guard error == nil, let returnData = data else {
completion(nil,error)
return
}

completion(TrackingInfo(returnData),nil)
}
}

private func getRequest(_ trackingNumber: String) -> String {
let APIUsername = "Intentionally Omitted"
let trackingXmlLink = "http://production.shippingapis.com/ShippingAPI.dll?API=TrackV2&XML=%3CTrackFieldRequest%20USERID=%22" + APIUsername + "%22%3E%20%3CRevision%3E1%3C/Revision%3E%20%3CClientIp%3E127.0.0.1%3C/ClientIp%3E%20%3CSourceId%3EFaiz%20Surani%3C/SourceId%3E%20%3CTrackID%20ID=%22" + trackingNumber + "%22%3E%20%3CDestinationZipCode%3E66666%3C/DestinationZipCode%3E%20%3CMailingDate%3E2010-01-01%3C/MailingDate%3E%20%3C/TrackID%3E%20%3C/TrackFieldRequest%3E"
return trackingXmlLink
}

public func callRestService(requestUrl:String, completion:(Data? , URLResponse? , Error? ) -> Void) ->Void
{
var request = URLRequest(url: URL(string: requestUrl)!)
request.httpMethod = "GET"

let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: completion)

task.resume()
}

关于ios - DispatchQueue 不等待异步函数完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48393805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com