【发布时间】:2016-12-05 16:48:45
【问题描述】:
我想知道为什么我的函数要花费这么多时间来构建。对我来说最长的时间是206.1ms,超过3分钟!
187721.1ms InspectionViewController.swift:423:17 @IBAction @objc func btnSaveTouch(_ sender: Any)
我一直在寻找一些优化,但找不到任何合理的原因导致我的功能如此缓慢。
我在这里使用 Alamofire,还有几个字段(文本字段、文本视图和 2 个开关)
代码如下:
@IBAction func btnSaveTouch(_ sender: Any) {
var two_storeys = ""
var legal_height_downstairs = ""
if s_two_storeys.isOn {
two_storeys = "Yes"
} else {
two_storeys = "No"
}
if s_legal_height_downstairs.isOn {
legal_height_downstairs = "Yes"
} else {
legal_height_downstairs = "No"
}
let parameters : Parameters = [
"inspection_date" : tf_inspection_date.text!,
"property_type" : tf_property_type.text!,
"beds" : tf_beds.text!,
"baths" : tf_baths.text!,
"cars" : tf_cars.text!,
"our_price_min" : tf_our_price_min.text!,
"our_price_max" : tf_our_price_max.text!,
"max_rent" : tf_rent_max.text!,
"min_rent" : tf_rent_min.text!,
"upfront_max": tf_upfront_max.text!,
"character": tf_character.text!,
"character_years": tf_character_years.text!,
"character_uers": tf_character_year.text!,
"build_construction": tf_build_construction.text!,
"roof_material": tv_roof_material.text!,
"house_on": tf_house_on.text!,
"two_storeys": two_storeys,
"legal_height_downstairs": legal_height_downstairs,
"legal_height_downstairs_value" : tv_legal_height_downstairs_value.text!
]
Alamofire.request(saveUrl, method: .post, parameters: parameters).responseJSON { response in
_ = handleError(response: response)
}
}
【问题讨论】:
-
最长构建时间真的是 206 毫秒吗?因为那还不到一秒。或者你的意思是说秒?
-
您是否尝试过使用邮递员等外部工具发出相同的请求?只是为了确定不是你的错,也许只是因为端点很慢
-
Swift 编译器不喜欢大字典。首先尝试创建空字典并将值添加到其中。就像
parameters["inspection_date"] = tf_inspection_date.text!然后是下一个值等等。我知道这并不好玩,但通常这是一个有效的解决方法。
标签: ios optimization swift3 alamofire