Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get from cache then network? #3

Closed
a1exb1 opened this issue Aug 13, 2015 · 3 comments
Closed

Get from cache then network? #3

a1exb1 opened this issue Aug 13, 2015 · 3 comments

Comments

@a1exb1
Copy link

a1exb1 commented Aug 13, 2015

How would i acheive this? I want to use cache to display whatever is available from cache on initial load, and then request the image from network if available - re-firing the same callback closure.

@kiavashfaisali
Copy link
Owner

Hello, thank you for this great question!

KFSwiftImageLoader doesn't currently have an easy way to do this as the emphasis is currently put on placeholder images.
Until the feature you mention is added to KFSwiftImageLoader, you could maintain a separate cache (NSURLCache, NSCache, etc.) strictly for placeholder purposes; i.e.

placeholderCache.cacheImage(imageView.image, forKey: urlString)



// The next time the app is launched, do something like:
imageView.loadImageFromURLString(urlString, placeholderImage: placeholderCache.cachedImageForKey(urlString)) {
    (finished, error) in

    // Cache the new image as a future placeholder.
    placeholderCache.cacheImage(imageView.image, forKey: urlString)
}

@a1exb1
Copy link
Author

a1exb1 commented Aug 24, 2015

Wouldnt the loadImageFromURLString return the already cached image in this case?

@kiavashfaisali
Copy link
Owner

Yes, sorry about that, I forgot to include the following line as well:

KFImageCacheManager.sharedInstance.diskCacheMaxAge = 0

You should put that line in application:DidFinishLaunchingWithOptions: so that the file cache is purged when the app loads for the first time, and then from there since you've maintained your own placeholderCache, you can pull the previously cached image and put it as a placeholder.

Since there won't be a memory cache on first load, the image won't be loaded from cache in this case (nor the file cache due to the above line).

This solution is only good for first time load scenarios. I'll be adding this feature to the next version of KFSwiftImageLoader in the coming weeks as I'm going to overhaul the tool for Swift 2.0 as well by then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants