Search code examples
swifthttp-postxcode7alamofire

How to make network request faster in Swift


I'm using Alamofire for network request. When I load a new viewController I make a new request in ViewDidAppear to get example url to images ect. When I make the request in ViewDidAppear there is a delay before the data appear, I also tried in ViewDidLoad the request was a little bit faster, but you can stil see the data appear after a small delay. It is okay that when a user access the viewController first time the user will see the data is loading, but is there a way to keep the data so that when a user navigate away from the controller, example when a user go back from a push in a navigationController and then navigate forward again without making the request to get the data again?

Here is one of my request in ViewDidAppear.

Hope you guys can help - Thank you

override func viewDidAppear(animated: Bool) {

    navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
    var parameters = [String: AnyObject]()

    if(self.MySelf) {
        parameters = ["userId": LoginViewController.CurrentUser.UserID as AnyObject]
    }
    else {
        parameters = ["userId": self.UserID as AnyObject]
    }


    //GET posts
    Alamofire.request(.POST, Config.ApiURL + "getUserPosts?token=" + LoginViewController.CurrentUser.Token, parameters: parameters as! [String : AnyObject]).responseJSON{ response in
        print(response)

        switch response.result {
        case .Success(let data):
            let json = JSON(data)
            if let posts = json["post"]["data"].array {
                self.postArray = posts
                self.postArray = self.postArray.reverse()

                self.navigationItem.title = json["user"]["firstname"].string! + " " + json["user"]["lastname"].string!

                self.User = json["user"]
                self.UserPic = self.User["photourl"].string!
            }
            else {
                print("Array is empty")
            }

        case .Failure(let error):
            print("Request failed with error: \(error)")
        }

        self.ProfilePostColView?.reloadData()
    }
}

Solution

  • Try making the network request in an earlier view controller - say a loading screen and then pass the response to this view controller.

    Alternatively you could store the response in a cache service of sorts - when the user navigates back to this view controller you could check it already in the cache if so load it up to the view if not call the request.

    Also making the network request in viewDidLoad will be faster as it called before viewDidAppear - but keep in mind viewDidLoad is only called once for a specific instance of a viewController where as viewDidAppear is called every time that instance is displayed again (eg. if it as the bottom of the navigation stack and the user presses back to it).

    Keep the user in mind - you do not want to be chewing up their data so if you know the request will have the same response you only want to make the http request once.