ios - 数据响应 Swift 4 Timer 时的 Alamofire
问题描述
我有一个 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()
}
推荐阅读
- c - arduino中的PID实现
- tensorflow - pytorch 等效 tf.gather
- c++ - PE SizeOfImage 错误
- reactjs - 使用带有 typescript 的 React 材料设计
- html - 应用 :hover 到多个#id
- javascript - 在 paper.js 上检测透明图像碰撞
- c - 在c中使用#if创建程序模式
- javascript - 在javascript中使用web3调用具有参数的智能合约函数
- ruby - 检查 PDF 文件是否被 Ruby 损坏?
- reactjs - 使用 react 和 redux/thunk 进行异步操作后的导航