gpt4 book ai didi

ios - 快速缓存.. iOS FBApi

转载 作者:行者123 更新时间:2023-11-28 08:36:18 26 4
gpt4 key购买 nike

我正在从 fbgraph api 获取数据并在 View 中显示。

获取代码如下。

 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))
}
}
}
}

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()
}

每当应用程序运行并开始显示时,大约需要 1.5 秒才能显示所有内容。您是否可以通过任何方式缓存此数据,因此无论何时出现屏幕,您都不必等待 2.0 秒,因为它会自动显示为 View 显示为其缓存。因为它是第一个出现的 View

最佳答案

您可以将这些内容保存在 NSUserDefaults 中,这样您就可以毫不延迟地轻松显示此信息。数据将在从 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))
}
}
}
}

关于ios - 快速缓存.. iOS FBApi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37603573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com