打网络电话的最佳场所 [英] Best place to make network calls

查看:151
本文介绍了打网络电话的最佳场所的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网络电话: -

   static func getProfile(parameters:[String:AnyObject], onComplete:[String:AnyObject]->()) {

        var requiredData:[String:AnyObject] = [:]

        Alamofire.request(.GET,API.getProfile,parameters: parameters).validate().responseJSON { (response) in
            if let responseData = response.result.value {
                if let jsonData = responseData as? [String:AnyObject] {
                    requiredData["UserName"] = jsonData["UName"]
                    requiredData["UserEmail"] = jsonData["UEmail"]
                    requiredData["UserMobileNo"] = jsonData["UPhone"]
                    requiredData["UserAddress"] = jsonData["UAddress"]
                    requiredData["UserCity"] = jsonData["UCity"]
                }// Inner If

            } // Main if
            onComplete(requiredData)
        }// Alamofire Closed

    }// Func closed

所需VC内的网络电话:

 override func viewDidLoad() {
        super.viewDidLoad()
        let parameters:[String:AnyObject] = [
            "WebKey": API.WebKey.value.rawValue,
            "UId":NSUserDefaults.standardUserDefaults().integerForKey("UserId")
        ]
        NetworkInterface.getProfile(parameters) { (responseDictionary) in
            //print("Passed Data \(responseDictionary["UserName"])")
            self.userData = responseDictionary
            self.updateUI()
        }

    }

据我所知,VC生命周期有点如下: -

As far as i know, VC Lifecycle is somewhat as follows :-

init(编码器aDecoder:NSCoder) - > viewDidLoad - > viewWillAppear - > viewWillDisappear

init(coder aDecoder:NSCoder) -> viewDidLoad -> viewWillAppear -> viewWillDisappear

然而,偶数在查看之后,用户信息在这些文本字段中显示需要几秒钟。我认为viewDidLoad是进行网络呼叫的最佳场所。

However, Even after view appears it takes few seconds for user Information to be displayed in those textfields. I thought viewDidLoad is the best place to make network calls.

我知道网络呼叫是异步的,因此需要时间从网络获取所需数据并做出响应。但是,在viewDidLoad中进行了网络调用,所以当时间视图出现时,它应该已经有了所需的数据?它应该不是吗?

所以有人可以解释一下哪个是拨打网络电话的最佳地点,为什么?我希望在视图出现时立即使用用户信息更新文本字段。

So can anyone explain me which is the best place to make network calls and why? I want textfields to be updated with user Info as soon as view Appears.

推荐答案

请求需要要在 viewWillAppear:中触发,只有这种方法会通知您屏幕即将显示。如果您不想在每次显示屏幕时发送请求,请考虑在数据显示后缓存数据。

Requests need to be fired in the viewWillAppear:, only this method notifies you that the screen is about to be shown. If you don't want to send requests every time the screen is shown, consider to cache the data once you have it.

viewDidLoad 不是最好的候选人。它与屏幕的外观无关。它是在第一次请求视图控制器视图后立即调用,而不是在屏幕显示时调用。

viewDidLoad is not the best candidate. It has nothing to do with appearance of the screen. It's called right after a view controller's view is requested for the first time, not when the screen is showing up.

例如,如果屏幕被破坏(通过弹出导航控制器),当你再次显示它时(通过将屏幕推到导航控制器),你将收到 viewDidLoad 。或者,如果应用程序收到内存警告,则会卸载并再次加载当前视图,最终会发送视图控制器 viewDidLoad

For example, if the screen was destroyed (by popping from a navigation controller), you'll receive viewDidLoad when you show it again (by pushing the screen to the navigation controller). Or if the app receives a memory warning, a current view is unloaded and loaded again, which ends up sending the view controller viewDidLoad.

viewDidLoad 很棘手。

如果您认为 viewDidLoad 将保护您多次从服务器获取数据:有时它会,有时它不会。无论如何,它不是优化网络的正确工具,缓存是!

If you think that viewDidLoad will safe you from fetching the data from the server multiple times: sometimes it will, sometimes it won't. Anyway it's not a right tool to optimize networking, caching is!

由于远程请求很昂贵(它们需要时间和流量),您需要了解它们何时发送。 viewWillAppear:让您了解。与缓存相结合,您可以使其达到最佳状态。

Since remote requests are expensive (they take time and traffic), you want understand when are they sent. viewWillAppear: gives you understanding. And in conjunction with caching you can make it optimal.

更新

In大多数情况下,直接从视图控制器发送请求不是一个好主意。我建议创建一个单独的网络层。

In most cases it's not a good idea to send requests from the view controller directly. I would suggest to create a separate networking layer.

这篇关于打网络电话的最佳场所的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆