【发布时间】:2019-03-05 04:04:43
【问题描述】:
我有一个 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
方法是否正确? 我觉得不安全。如果不正确,我可以使用什么?
【问题讨论】:
-
这不是正确的方法。您必须使用完成处理程序或调用
reloadData而不是print(Common.Customers)。并且不要在 Swift 中使用NSDictionary。
标签: ios swift uitableview asynchronous alamofire