Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Displaying API Data with Collection Views in Objective-C Show Me the Data - API’s, OAuth, and NSURLSession Caching our Photos

David Lin
David Lin
35,864 Points

SAMCache does not work with Foursquare photo url as key

The caching as implemented in the video Caching our Photos does not work.

After debugging, I discovered that keys with characters before a forward slash will not work, for example, keys consisting of URLs: "http://foursquare.com/image.jpg". Since the urlStrings of the Foursquare photos were used as each photo's key, SAMCache does not recognize the key for some reason, and the photo is never retrieved from cache.

Interestingly, as long as there's no character in front of a forward slash, the key works, e.g. "/image.jpg" works ok.

I opened an issue about this at SAMCache's git repo:
https://github.com/soffes/SAMCache/issues/20

Here is a fix for the photo caching, where I explicitly remove the forward slashes from the urlString to form the key:

-(void)downloadPhotoWithURL:(NSString*) urlString{
    // *** NEED TO REMOVE FORWARD SLASHES TO FORM A VALID KEY THAT WORKS WITH SAMCACHE
    NSString *key = [urlString stringByReplacingOccurrencesOfString:@"/" withString:@""]; 
    UIImage *cachedPhoto = [[SAMCache sharedCache] imageForKey:key];

    if(cachedPhoto){
        self.photoView.image = cachedPhoto;
        return;
    }

... etc ...

}
Roger Antonell
Roger Antonell
18,252 Points

Thanks you again , totally right

David Lin
David Lin
35,864 Points

You're welcome, Roger.