1

I am fetching from the fbgraph api and showing in a view.

the code for fetching is below.

 func fetchProfile(){
        let parameters = ["fields":"email, name, picture.type(large)"]
        FBSDKGraphRequest(graphPath: "me", parameters: parameters).startWithCompletionHandler { (connection, result, error) in
            if error != nil{
                print(error)
                return
            } else {
                print(result)
                self.nameLabel.text = result["name"] as? NSString as? String
                self.emailAddress.text = result["email"] as? NSString as? String
                if let picture = result["picture"] as? NSDictionary, data = picture["data"] as? NSDictionary, url = data["url"] as? String{
                    print("this is what yout fetch with \(url)")
                    self.imageView.sd_setImageWithURL(NSURL(string: url))
                }
           }
        }
    }

The Code in viewDidLoad

 override func viewDidLoad() {
        super.viewDidLoad()
        self.imageView.clipsToBounds = true
        self.imageView.layer.masksToBounds = true
        self.imageView.layer.cornerRadius = self.imageView.frame.height/2
        self.fetchProfile() 
 }

Whenever the application runs and it starts to show it takes around 1.5 seconds to show all of it. is thier any way you can cache this data so whenever the screen comes up you dont have to wait 2.0 seconds as it automatically shows up as the view shows up as its caches. as it is the fist view to show up

Mudith Chathuranga Silva
  • 7,253
  • 2
  • 50
  • 58
Aryan Kashyap
  • 121
  • 1
  • 12

1 Answers1

0

You can save those things in NSUserDefaults, the you can easily show this infor without any delay. Data will update after the real data fecth from the FBGraphAPI.

  func fetchProfile(){
    let defaults = NSUserDefaults.standardUserDefaults()
    if let _ = userDefaults.valueForKey("userName") {
        self.nameLabel.text = (userDefaults.valueForKey("userName") as? String)!
    }
    if let _ = userDefaults.valueForKey("userEmail") {
        self.emailAddress.text = (userDefaults.valueForKey("userEmail") as? String)!
    }
    let parameters = ["fields":"email, name, picture.type(large)"]
    FBSDKGraphRequest(graphPath: "me", parameters: parameters).startWithCompletionHandler { (connection, result, error) in
        if error != nil{

        } else {
            let userName: String! = result["name"] as? NSString as? String
            defaults.setObject(userName, forKey: "userName")
            self.nameLabel.text = userName
            let userEmail: String! = result["email"] as? NSString as? String
            defaults.setObject(userEmail, forKey: "userEmail")

            if let picture = result["picture"] as? NSDictionary, data = picture["data"] as? NSDictionary, url = data["url"] as? String{
                defaults.setObject(data!, forKey: "userPictureData")
                self.imageView.sd_setImageWithURL(NSURL(string: url))
            }
       }
    }
}
Mudith Chathuranga Silva
  • 7,253
  • 2
  • 50
  • 58