I have an Alamofire function like when data comes to insert data into Global NsDictionary
Common.Customers
Function is
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)
}
}
This code is working. But, if I didn't use the timer Common.Customers
always nill.
When ı want to call this function, I am calling like
WebService.PostAlomofire(format: format)
_ = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { timer in
self.Table_tv.reloadData()
}
but if data were not coming 0.5 seconds, this code doesn't work.
Timer.scheduledTimer
Is correct way or not? It doesn't feel safe to me. What can I use if it is not true?
You need to create a completion handler as this is not the job of a timer
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()
}