【问题标题】:long build time for simple function简单功能的构建时间长
【发布时间】: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


【解决方案1】:

所以 Eric Aya 是对的。现在这个函数的编译时间是 19.9ms。工作代码如下。

    var paramaters : Parameters = [:]

    paramaters["inspection_date"] = tf_inspection_date.text!
    paramaters["property_type"] = tf_property_type.text!
    paramaters["beds"] = tf_beds.text!
    paramaters["baths"] = tf_baths.text!
    paramaters["cars"] = tf_cars.text!
    paramaters["our_price_min"] = tf_our_price_min.text!
    paramaters["our_price_max"] = tf_our_price_max.text!
    paramaters["max_rent"] = tf_rent_max.text!
    paramaters["min_rent"] = tf_rent_min.text!
    paramaters["upfront_max"] = tf_upfront_max.text!
    paramaters["character"] = tf_character.text!
    paramaters["character_years"] = tf_character_years.text!
    paramaters["character_uers"] = tf_character_year.text!
    paramaters["build_construction"] = tf_build_construction.text!
    paramaters["roof_material"] = tv_roof_material.text!
    paramaters["house_on"] = tf_house_on.text!
    paramaters["two_storeys"] = two_storeys
    paramaters["legal_height_downstairs"] = legal_height_downstairs
    paramaters["legal_height_downstairs_value"] = tv_legal_height_downstairs_value.text!

回答 Jeremy 的问题 - 是的,代码非常简单:

【讨论】:

    猜你喜欢
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    相关资源
    最近更新 更多