Best place to make network calls

2019-02-24 18:17发布

Network Call :-

   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

Network Call within required 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()
        }

    }

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

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

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.

I understand that network calls are async so it will take time to fetch required data from network and respond. However, network call was made in viewDidLoad so by the time view will appear, it should already have required data ? Should it not ?

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.

2条回答
forever°为你锁心
2楼-- · 2019-02-24 18:26

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 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.

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 is tricky.

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!

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.

UPDATE

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.

查看更多
一纸荒年 Trace。
3楼-- · 2019-02-24 18:40

I think viewDidLoad is the correct place to make the network call if it fits that screen's need. i.e. you don't have to re-request the data at some point. For example if profile data has changed since the view was loaded.

As for network requests taking time, it's possible that your view appears before the network request is done. I suggest adding some loading indicator that you hide after the request completed.

Also, keep in mind that network requests can fail so you should deal with that by retrying the request or displaying an error message.

查看更多
登录 后发表回答