【问题标题】:Issues with calling an API taking too much time to get a response调用 API 需要花费太多时间才能获得响应的问题
【发布时间】:2019-02-28 21:29:42
【问题描述】:

我在 iOS (swift) 中调用 API。一切都很完美,但是在大约 40 或 60 秒内得到响应会花费太多时间。我不知道为什么会这样。让我给你看一下我的API调用方法:

代码

func userDetailAPI(){

    let preferences = UserDefaults.standard

    let uid = "u_id"
    let acctkn = "acc_tkn"


    if preferences.object(forKey: uid) == nil {
        //  Doesn't exist
    } else {
        let u_id = preferences.object(forKey: uid) as! String
        print(u_id)
        let acc_tkn = preferences.object(forKey: acctkn) as! String
        print(acc_tkn)

        let userprofile: [String : Any] = ["user_id":u_id,"access_token":acc_tkn]
        print(userprofile)
        Alamofire.request(userDetails, method: .post, parameters: userprofile).responseJSON { response in
                print("RESPONSE : \(response)")
                let result = response.result.value

            if result != nil{
                let data = result as! [String : AnyObject]


                let userdata = data["data"] as! NSDictionary

                let email = userdata["email"]
                let name = userdata["name"]
                let photo = userdata["photo"]
                //let u_type = userdata["user_type"]!
                self.lblUserName.text = name as? String
                self.lblEmailID.text = email as? String

                let proimgurl = NSURL(string: photo as! String)
                self.imgProPic.image = UIImage(data: NSData(contentsOf: proimgurl! as URL)! as Data)

                //  }
        }

    }
}
}

请检查并帮助我 - 这是 API 调用的正确方法还是有其他更好的方法?

【问题讨论】:

  • 不相关但不要在 Swift 中使用 NS 类,如果有本地对应类(NSURLNSDataNSDictionary)。在 Swift 3+ 中,JSON 值永远不会是 AnyObject

标签: ios swift api alamofire swifty-json


【解决方案1】:

因为这条线

self.imgProPic.image = UIImage(data: NSData(contentsOf: proimgurl! as URL)! as Data)

所以你有 almofire 请求加上阻塞主线程直到图像下载,所以考虑使用异步自动兑现SDWebImage

self.imgProPic.sd_setImage(with: proimgurl!, placeholderImage: UIImage(named: "placeholder.png"))

同时避免使用 NS 类似这里的东西

let userdata = data["data"] as! NSDictionary // use [String:Any]

let proimgurl = NSURL(string: photo as! String) // use URL

【讨论】:

  • AlamofireImage 也可能是一个好主意,因为它是相关的第三方。
  • "避免使用 NS"??那么我将如何使用 Core Data 对象?! :(
  • @J.Doe 尽可能避免NSStuff(通常是因为存在“Swift 等价物”:NSDictionary => Dictionary, NSJSONSerialization => JSONSerialization...
  • @J.Doe swift 不仅在 coreData 还在自动布局中进行了持续改进 NSLayoutConstraint 和其他东西,我说如果可用的话就使用它
【解决方案2】:

您应该在另一个线程中从 Url 下载 ImageView 的图像。如果您在主线程中执行此操作,它会减慢您的应用程序并最终耗尽内存。

下面给出的行是导致问题的原因

self.imgProPic.image = UIImage(data: NSData(contentsOf: proimgurl! as URL)! as Data)

我建议您使用SDWebImage 库。

你可以像下面这样做

let imageUrl = URL(string: photo as! String)
self.imgProPic.image.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: "profile"), options: .refreshCached, completed: nil)

如果这不能解决您的问题,请尝试使用 API 客户端(例如 Postman)调用相同的 Web 服务。如果花费相同的时间,那么您将无能为力。要求 Web 服务开发人员优化性能。

【讨论】:

    【解决方案3】:

    嘿,顺便说一下,还有 alamofire 图像 pod 可用。 https://github.com/Alamofire/AlamofireImage

    例如: import AlamofireImage 到您的文件中并调用图像网址,如下所示:

    Alamofire.request(image_url, method: .get).responseImage(completionHandler: { (response) in
                            self.your_UIImage_variable.image = response.result.value
                        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-17
      • 2018-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多