9
私は現在、swift 3.0にアップグレードしています。現在動作しているNSCacheを取得できません。私は何も見えないIm Im Miss私は間違って何をしているのか正確にはわからない。事前にありがとうswift 3 NSCacheが機能しません
class myClass {
let imageCache = NSCache()
func downloadImageByUserId(userId:String,imageView:UIImageView){
print(userId)
fireBaseAPI().childRef("version_one/frontEnd/users/\(userId)").observeEventType(.Value, withBlock: {snapshot in
let imageUrl = snapshot.value!["profileImageUrl"] as! String
// check image cache
print(self.imageCache.objectForKey(imageUrl))
if let cacheImage = self.imageCache.objectForKey(imageUrl) as? UIImage{
print("image cache")
imageView.image = cacheImage
return
}
print("image not cache")
//retrieve image
let url = NSURL(string: imageUrl)
NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, resposn, error) in
if error != nil {
print(error)
return
}
dispatch_async(dispatch_get_main_queue(),{
if let downloadImage:UIImage = UIImage(data: data!){
self.imageCache.setObject(downloadImage, forKey: imageUrl)
imageView.image = downloadImage
}
})
}).resume()
})
}
}
です:それは迅速
Dictionary
のような役割を果たし、あなたは
Key
とValue
のタイプを与える必要がありますか?私が間違っていない限り、メソッドが呼び出されるたびに、 'let imageCache = NSCache()'に新しい* empty *キャッシュを作成しています。 –@MartinRメソッドの外に 'let imageCache = NSCache()'をチェックして配置しました。それは 'NSUrlSession'の中で' nil'になったときだけキャッシュが動作するようにも見えます。 '.resume'と何か関係がないのかどうかは分かりません。修正の質問を見てください。 – pprevalon