gpt4 book ai didi

ios - Alamofire 数据响应时 Swift 4 Timer

转载 作者:可可西里 更新时间:2023-11-01 00:54:32 25 4
gpt4 key购买 nike

我有一个 Alamofire 函数,就像当数据来时将数据插入 Global NsDictionary

Common.Customers

函数是

static func PostAlomofire(format : RequestFormat)  {

let loginParam: [String: Any] = [
"searchTerm": format.Name,
"pageSize": format.PageSize,
"pageNumber": format.PageNumber ,
"deviceId": format.DeviceId
]
print(loginParam)

Alamofire.request("http://111.3.4.2/website/api/Customer/Search", method: .post, parameters: loginParam, encoding: JSONEncoding.prettyPrinted)
.responseJSON { response in
let result = response.result
print(result.value)
if let dict = result.value as? Dictionary<String,AnyObject>
{
if let innerDic = dict["results"]
{

Common.Customers = innerDic as! [NSDictionary]
}
}
print(Common.Customers)
}

}

此代码有效。但是,如果我不使用计时器 Common.Customers 总是无效的。当我想调用这个函数时,我会这样调用

  WebService.PostAlomofire(format: format)
_ = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { timer in
self.Table_tv.reloadData()

}

但如果数据未在 0.5 秒内到达,则此代码不起作用。

Timer.scheduledTimer

方法对不对?我觉得不安全。如果不是真的,我能用什么?

最佳答案

您需要创建一个完成处理程序,因为这不是计时器的工作

static func PostAlomofire(format : RequestFormat,completion:@escaping(()-> Void)) {
let loginParam: [String: Any] = [
"searchTerm": format.Name,
"pageSize": format.PageSize,
"pageNumber": format.PageNumber ,
"deviceId": format.DeviceId
]
print(loginParam)

Alamofire.request("http://111.3.4.2/website/api/Customer/Search", method: .post, parameters: loginParam, encoding: JSONEncoding.prettyPrinted)
.responseJSON { response in
let result = response.result
print(result.value)
if let dict = result.value as? [String: Any]
{
if let innerDic = dict["results"]
{

Common.Customers = innerDic as! [[String: Any]]
completion()
}
}
print(Common.Customers)
}
}

//

WebService.PostAlomofire(format: format) {
self.Table_tv.reloadData()
}

关于ios - Alamofire 数据响应时 Swift 4 Timer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52567505/

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